2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include "afs/param.h"
16 #ifdef AFS_LINUX24_ENV
17 #include "h/module.h" /* early to avoid printf->printk mapping */
19 #include "afs/sysincludes.h" /* Standard vendor system headers */
20 #include "afsincludes.h" /* Afs-based standard headers */
21 #include "afs/afs_stats.h" /* afs statistics */
22 #include "h/smp_lock.h"
23 #if defined(AFS_LINUX26_ENV)
26 #if !defined(HAVE_IGET)
27 #include "h/exportfs.h"
30 afs_lock_t afs_xosi; /* lock is for tvattr */
31 extern struct osi_dev cacheDev;
32 #if defined(AFS_LINUX24_ENV)
33 extern struct vfsmount *afs_cacheMnt;
35 extern struct super_block *afs_cacheSBp;
37 #if defined(AFS_LINUX26_ENV)
39 #if defined(LINUX_USE_FH)
40 osi_UFSOpen_fh(struct fid *fh, int fh_type)
42 osi_UFSOpen(afs_int32 ainode)
45 register struct osi_file *afile = NULL;
46 extern int cacheDiskType;
47 struct inode *tip = NULL;
48 struct dentry *dp = NULL;
49 struct file *filp = NULL;
50 #if !defined(HAVE_IGET) || defined(LINUX_USE_FH)
53 AFS_STATCNT(osi_UFSOpen);
54 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
55 osi_Panic("UFSOpen called for non-UFS cache\n");
57 if (!afs_osicred_initialized) {
58 /* valid for alpha_osf, SunOS, Ultrix */
59 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
60 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
61 afs_osicred_initialized = 1;
63 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
66 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
67 sizeof(struct osi_file));
69 memset(afile, 0, sizeof(struct osi_file));
70 #if defined(HAVE_IGET)
71 tip = iget(afs_cacheSBp, (u_long) ainode);
73 osi_Panic("Can't get inode %d\n", ainode);
75 dp = d_alloc_anon(tip);
77 #if defined(LINUX_USE_FH)
78 dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, fh, sizeof(struct fid), fh_type);
82 dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &fid, sizeof(fid), FILEID_INO32_GEN);
85 osi_Panic("Can't get dentry\n");
88 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
90 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
92 #if defined(LINUX_USE_FH)
93 osi_Panic("Can't open file\n");
95 osi_Panic("Can't open inode %d\n", ainode);
98 afile->size = FILE_INODE(filp)->i_size;
101 afile->proc = (int (*)())0;
102 #if defined(LINUX_USE_FH)
103 afile->inum = tip->i_ino; /* for hint validity checking */
105 afile->inum = ainode; /* for hint validity checking */
107 return (void *)afile;
111 osi_UFSOpen(afs_int32 ainode)
113 register struct osi_file *afile = NULL;
114 extern int cacheDiskType;
116 struct inode *tip = NULL;
117 struct file *filp = NULL;
118 AFS_STATCNT(osi_UFSOpen);
119 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
120 osi_Panic("UFSOpen called for non-UFS cache\n");
122 if (!afs_osicred_initialized) {
123 /* valid for alpha_osf, SunOS, Ultrix */
124 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
125 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
126 afs_osicred_initialized = 1;
128 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
131 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
132 sizeof(struct osi_file));
134 memset(afile, 0, sizeof(struct osi_file));
136 filp->f_dentry = &afile->dentry;
137 tip = iget(afs_cacheSBp, (u_long) ainode);
139 osi_Panic("Can't get inode %d\n", ainode);
140 FILE_INODE(filp) = tip;
141 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
142 filp->f_flags = O_RDWR;
143 #if defined(AFS_LINUX24_ENV)
144 filp->f_mode = FMODE_READ|FMODE_WRITE;
145 filp->f_op = fops_get(tip->i_fop);
147 filp->f_op = tip->i_op->default_file_ops;
149 if (filp->f_op && filp->f_op->open)
150 code = filp->f_op->open(tip, filp);
152 osi_Panic("Can't open inode %d\n", ainode);
153 afile->size = tip->i_size;
156 afile->proc = (int (*)())0;
157 afile->inum = ainode; /* for hint validity checking */
158 return (void *)afile;
162 #if defined(LINUX_USE_FH)
164 osi_get_fh(struct dentry *dp, struct fid *fh, int *max_len) {
167 if (dp->d_sb->s_export_op->encode_fh) {
168 fh_type = dp->d_sb->s_export_op->encode_fh(dp, (__u32 *)fh, max_len, 0);
170 fh_type = FILEID_INO32_GEN;
171 fh->i32.ino = dp->d_inode->i_ino;
172 fh->i32.gen = dp->d_inode->i_generation;
180 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
182 register afs_int32 code;
183 AFS_STATCNT(osi_Stat);
184 MObtainWriteLock(&afs_xosi, 320);
185 astat->size = OSIFILE_INODE(afile)->i_size;
186 #if defined(AFS_LINUX26_ENV)
187 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
188 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
190 astat->mtime = OSIFILE_INODE(afile)->i_mtime;
191 astat->atime = OSIFILE_INODE(afile)->i_atime;
194 MReleaseWriteLock(&afs_xosi);
198 #ifdef AFS_LINUX26_ENV
200 osi_UFSClose(register struct osi_file *afile)
202 AFS_STATCNT(osi_Close);
204 if (OSIFILE_INODE(afile)) {
205 filp_close(afile->filp, NULL);
209 osi_FreeLargeSpace(afile);
214 osi_UFSClose(register struct osi_file *afile)
216 AFS_STATCNT(osi_Close);
218 if (FILE_INODE(&afile->file)) {
219 struct file *filp = &afile->file;
220 if (filp->f_op && filp->f_op->release)
221 filp->f_op->release(FILE_INODE(filp), filp);
222 iput(FILE_INODE(filp));
226 osi_FreeLargeSpace(afile);
232 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
234 register afs_int32 code;
235 struct osi_stat tstat;
236 struct iattr newattrs;
237 struct inode *inode = OSIFILE_INODE(afile);
238 AFS_STATCNT(osi_Truncate);
240 /* This routine only shrinks files, and most systems
241 * have very slow truncates, even when the file is already
242 * small enough. Check now and save some time.
244 code = afs_osi_Stat(afile, &tstat);
245 if (code || tstat.size <= asize)
247 MObtainWriteLock(&afs_xosi, 321);
249 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
250 down_write(&inode->i_alloc_sem);
252 #ifdef STRUCT_INODE_HAS_I_MUTEX
253 mutex_lock(&inode->i_mutex);
257 newattrs.ia_size = asize;
258 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
259 #if defined(AFS_LINUX24_ENV)
260 newattrs.ia_ctime = CURRENT_TIME;
262 /* avoid notify_change() since it wants to update dentry->d_parent */
264 code = inode_change_ok(inode, &newattrs);
266 #ifdef INODE_SETATTR_NOT_VOID
267 code = inode_setattr(inode, &newattrs);
269 inode_setattr(inode, &newattrs);
273 truncate_inode_pages(&inode->i_data, asize);
275 inode->i_size = asize;
276 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
277 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
280 truncate_inode_pages(inode, asize);
281 if (inode->i_op && inode->i_op->truncate)
282 inode->i_op->truncate(inode);
286 #ifdef STRUCT_INODE_HAS_I_MUTEX
287 mutex_unlock(&inode->i_mutex);
291 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
292 up_write(&inode->i_alloc_sem);
295 MReleaseWriteLock(&afs_xosi);
300 /* Generic read interface */
302 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
309 AFS_STATCNT(osi_Read);
312 * If the osi_file passed in is NULL, panic only if AFS is not shutting
313 * down. No point in crashing when we are already shutting down
316 if (!afs_shuttingdown)
317 osi_Panic("osi_Read called with null param");
323 afile->offset = offset;
324 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
326 code = osi_rdwr(afile, &auio, UIO_READ);
329 code = asize - auio.uio_resid;
330 afile->offset += code;
332 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
333 ICL_TYPE_INT32, code);
339 /* Generic write interface */
341 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
348 AFS_STATCNT(osi_Write);
351 if (!afs_shuttingdown)
352 osi_Panic("afs_osi_Write called with null param");
358 afile->offset = offset;
359 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
361 code = osi_rdwr(afile, &auio, UIO_WRITE);
364 code = asize - auio.uio_resid;
365 afile->offset += code;
369 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
374 (*afile->proc)(afile, code);
380 /* This work should be handled by physstrat in ca/machdep.c.
381 This routine written from the RT NFS port strategy routine.
382 It has been generalized a bit, but should still be pretty clear. */
384 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
386 afs_int32 returnCode;
388 AFS_STATCNT(osi_MapStrategy);
389 returnCode = (*aproc) (bp);
395 shutdown_osifile(void)
397 AFS_STATCNT(shutdown_osifile);
398 if (afs_cold_shutdown) {
399 afs_osicred_initialized = 0;
403 /* Intialize cache device info and fragment size for disk cache partition. */
405 osi_InitCacheInfo(char *aname)
408 #if defined(LINUX_USE_FH)
409 int max_len = sizeof(struct fid);
411 extern ino_t cacheInode;
414 extern ino_t cacheInode;
415 extern struct osi_dev cacheDev;
416 extern afs_int32 afs_fsfragsize;
417 extern struct super_block *afs_cacheSBp;
418 extern struct vfsmount *afs_cacheMnt;
419 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
423 #if defined(LINUX_USE_FH)
424 if (dp->d_sb->s_export_op->encode_fh) {
425 cacheitems_fh_type = dp->d_sb->s_export_op->encode_fh(dp, (__u32 *)&cacheitems_fh, &max_len, 0);
427 cacheitems_fh_type = FILEID_INO32_GEN;
428 cacheitems_fh.i32.ino = dp->d_inode->i_ino;
429 cacheitems_fh.i32.gen = dp->d_inode->i_generation;
432 cacheInode = dp->d_inode->i_ino;
434 cacheDev.dev = dp->d_inode->i_sb->s_dev;
435 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
436 afs_cacheSBp = dp->d_inode->i_sb;
444 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
445 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
448 * seek, then read or write to an open inode. addrp points to data in
452 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
454 #ifdef AFS_LINUX26_ENV
455 struct file *filp = osifile->filp;
457 struct file *filp = &osifile->file;
463 unsigned long savelim;
465 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
466 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
468 if (uiop->uio_seg == AFS_UIOSYS)
471 /* seek to the desired position. Return -1 on error. */
472 if (filp->f_op->llseek) {
473 if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
476 filp->f_pos = uiop->uio_offset;
478 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
480 count = iov->iov_len;
488 code = FOP_READ(filp, iov->iov_base, count);
490 code = FOP_WRITE(filp, iov->iov_base, count);
495 } else if (code == 0) {
497 * This is bad -- we can't read any more data from the
498 * file, but we have no good way of signaling a partial
505 iov->iov_base += code;
506 iov->iov_len -= code;
507 uiop->uio_resid -= code;
508 uiop->uio_offset += code;
512 if (uiop->uio_seg == AFS_UIOSYS)
515 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
521 * Setup a uio struct.
524 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
525 int count, uio_flag_t flag, uio_seg_t seg)
527 iovecp->iov_base = (char *)buf;
528 iovecp->iov_len = count;
529 uiop->uio_iov = iovecp;
530 uiop->uio_iovcnt = 1;
531 uiop->uio_offset = pos;
533 uiop->uio_resid = count;
534 uiop->uio_flag = flag;
539 * UIO_READ : dp -> uio
540 * UIO_WRITE : uio -> dp
543 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
549 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
551 count = iov->iov_len;
562 switch (uiop->uio_seg) {
566 memcpy(iov->iov_base, dp, count);
569 memcpy(dp, iov->iov_base, count);
572 printf("uiomove: Bad rw = %d\n", rw);
579 AFS_COPYOUT(dp, iov->iov_base, count, code);
582 AFS_COPYIN(iov->iov_base, dp, count, code);
585 printf("uiomove: Bad rw = %d\n", rw);
590 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
596 iov->iov_base += count;
597 iov->iov_len -= count;
598 uiop->uio_offset += count;
599 uiop->uio_resid -= count;