vfs.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,815 行 · 第 1/3 页
C
1,815 行
case nfs_ok: result |= map->access; break; /* the following error codes just mean the access was not allowed, * rather than an error occurred */ case nfserr_rofs: case nfserr_acces: case nfserr_perm: /* simply don't "or" in the access bit. */ break; default: error = err2; goto out; } } } *access = result; if (supported) *supported = sresult; out: return error;}#endif /* CONFIG_NFSD_V3 *//* * Open an existing file or directory. * The access argument indicates the type of open (read/write/lock) * N.B. After this call fhp needs an fh_put */intnfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access, struct file **filp){ struct dentry *dentry; struct inode *inode; int flags = O_RDONLY|O_LARGEFILE, err; /* * If we get here, then the client has already done an "open", * and (hopefully) checked permission - so allow OWNER_OVERRIDE * in case a chmod has now revoked permission. */ err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE); if (err) goto out; dentry = fhp->fh_dentry; inode = dentry->d_inode; /* Disallow access to files with the append-only bit set or * with mandatory locking enabled */ err = nfserr_perm; if (IS_APPEND(inode) || IS_ISMNDLK(inode)) goto out; if (!inode->i_fop) goto out; /* * Check to see if there are any leases on this file. * This may block while leases are broken. */ err = break_lease(inode, O_NONBLOCK | ((access & MAY_WRITE) ? FMODE_WRITE : 0)); if (err) /* NOMEM or WOULDBLOCK */ goto out_nfserr; if (access & MAY_WRITE) { flags = O_WRONLY|O_LARGEFILE; DQUOT_INIT(inode); } *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_mnt), flags); if (IS_ERR(*filp)) err = PTR_ERR(*filp);out_nfserr: if (err) err = nfserrno(err);out: return err;}/* * Close a file. */voidnfsd_close(struct file *filp){ fput(filp);}/* * Sync a file * As this calls fsync (not fdatasync) there is no need for a write_inode * after it. */inline void nfsd_dosync(struct file *filp, struct dentry *dp, struct file_operations *fop){ struct inode *inode = dp->d_inode; int (*fsync) (struct file *, struct dentry *, int); filemap_fdatawrite(inode->i_mapping); if (fop && (fsync = fop->fsync)) fsync(filp, dp, 0); filemap_fdatawait(inode->i_mapping);} voidnfsd_sync(struct file *filp){ struct inode *inode = filp->f_dentry->d_inode; dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name); down(&inode->i_sem); nfsd_dosync(filp, filp->f_dentry, filp->f_op); up(&inode->i_sem);}voidnfsd_sync_dir(struct dentry *dp){ nfsd_dosync(NULL, dp, dp->d_inode->i_fop);}/* * Obtain the readahead parameters for the file * specified by (dev, ino). */static spinlock_t ra_lock = SPIN_LOCK_UNLOCKED;static inline struct raparms *nfsd_get_raparms(dev_t dev, ino_t ino){ struct raparms *ra, **rap, **frap = NULL; int depth = 0; spin_lock(&ra_lock); for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) { if (ra->p_ino == ino && ra->p_dev == dev) goto found; depth++; if (ra->p_count == 0) frap = rap; } depth = nfsdstats.ra_size*11/10; if (!frap) { spin_unlock(&ra_lock); return NULL; } rap = frap; ra = *frap; ra->p_dev = dev; ra->p_ino = ino; ra->p_set = 0;found: if (rap != &raparm_cache) { *rap = ra->p_next; ra->p_next = raparm_cache; raparm_cache = ra; } ra->p_count++; nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++; spin_unlock(&ra_lock); return ra;}/* * Grab and keep cached pages assosiated with a file in the svc_rqst * so that they can be passed to the netowork sendmsg/sendpage routines * directrly. They will be released after the sending has completed. */static intnfsd_read_actor(read_descriptor_t *desc, struct page *page, unsigned long offset , unsigned long size){ unsigned long count = desc->count; struct svc_rqst *rqstp = desc->arg.data; if (size > count) size = count; if (rqstp->rq_res.page_len == 0) { get_page(page); rqstp->rq_respages[rqstp->rq_resused++] = page; rqstp->rq_res.page_base = offset; rqstp->rq_res.page_len = size; } else if (page != rqstp->rq_respages[rqstp->rq_resused-1]) { get_page(page); rqstp->rq_respages[rqstp->rq_resused++] = page; rqstp->rq_res.page_len += size; } else { rqstp->rq_res.page_len += size; } desc->count = count - size; desc->written += size; return size;}/* * Read data from a file. count must contain the requested read count * on entry. On return, *count contains the number of bytes actually read. * N.B. After this call fhp needs an fh_put */intnfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset, struct kvec *vec, int vlen, unsigned long *count){ struct raparms *ra; mm_segment_t oldfs; int err; struct file *file; struct inode *inode; err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file); if (err) goto out; err = nfserr_perm; inode = file->f_dentry->d_inode;#ifdef MSNFS if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) && (!lock_may_read(inode, offset, *count))) goto out_close;#endif /* Get readahead parameters */ ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino); if (ra && ra->p_set) file->f_ra = ra->p_ra; if (file->f_op->sendfile) { svc_pushback_unused_pages(rqstp); err = file->f_op->sendfile(file, &offset, *count, nfsd_read_actor, rqstp); } else { oldfs = get_fs(); set_fs(KERNEL_DS); err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset); set_fs(oldfs); } /* Write back readahead params */ if (ra) { spin_lock(&ra_lock); ra->p_ra = file->f_ra; ra->p_set = 1; ra->p_count--; spin_unlock(&ra_lock); } if (err >= 0) { nfsdstats.io_read += err; *count = err; err = 0; dnotify_parent(file->f_dentry, DN_ACCESS); } else err = nfserrno(err);out_close: nfsd_close(file);out: return err;}/* * Write data to a file. * The stable flag requests synchronous writes. * N.B. After this call fhp needs an fh_put */intnfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset, struct kvec *vec, int vlen, unsigned long cnt, int *stablep){ struct svc_export *exp; struct file *file; struct dentry *dentry; struct inode *inode; mm_segment_t oldfs; int err = 0; int stable = *stablep; err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file); if (err) goto out; if (!cnt) goto out_close; err = nfserr_perm;#ifdef MSNFS if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) && (!lock_may_write(file->f_dentry->d_inode, offset, cnt))) goto out_close;#endif dentry = file->f_dentry; inode = dentry->d_inode; exp = fhp->fh_export; /* * Request sync writes if * - the sync export option has been set, or * - the client requested O_SYNC behavior (NFSv3 feature). * - The file system doesn't support fsync(). * When gathered writes have been configured for this volume, * flushing the data to disk is handled separately below. */ if (file->f_op->fsync == 0) {/* COMMIT3 cannot work */ stable = 2; *stablep = 2; /* FILE_SYNC */ } if (!EX_ISSYNC(exp)) stable = 0; if (stable && !EX_WGATHER(exp)) file->f_flags |= O_SYNC; /* Write the data. */ oldfs = get_fs(); set_fs(KERNEL_DS); err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset); set_fs(oldfs); if (err >= 0) { nfsdstats.io_write += cnt; dnotify_parent(file->f_dentry, DN_MODIFY); } /* clear setuid/setgid flag after write */ if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) { struct iattr ia; ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID; down(&inode->i_sem); notify_change(dentry, &ia); up(&inode->i_sem); } if (err >= 0 && stable) { static ino_t last_ino; static dev_t last_dev; /* * Gathered writes: If another process is currently * writing to the file, there's a high chance * this is another nfsd (triggered by a bulk write * from a client's biod). Rather than syncing the * file with each write request, we sleep for 10 msec. * * I don't know if this roughly approximates * C. Juszak's idea of gathered writes, but it's a * nice and simple solution (IMHO), and it seems to * work:-) */ if (EX_WGATHER(exp)) { if (atomic_read(&inode->i_writecount) > 1 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) { dprintk("nfsd: write defer %d\n", current->pid); set_current_state(TASK_UNINTERRUPTIBLE); schedule_timeout((HZ+99)/100); dprintk("nfsd: write resume %d\n", current->pid); } if (inode->i_state & I_DIRTY) { dprintk("nfsd: write sync %d\n", current->pid); nfsd_sync(file); }#if 0 wake_up(&inode->i_wait);#endif } last_ino = inode->i_ino; last_dev = inode->i_sb->s_dev; } dprintk("nfsd: write complete err=%d\n", err); if (err >= 0) err = 0; else err = nfserrno(err);out_close: nfsd_close(file);out: return err;}#ifdef CONFIG_NFSD_V3/* * Commit all pending writes to stable storage. * Strictly speaking, we could sync just the indicated file region here, * but there's currently no way we can ask the VFS to do so. * * Unfortunately we cannot lock the file to make sure we return full WCC * data to the client, as locking happens lower down in the filesystem. */intnfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset, unsigned long count){ struct file *file; int err; if ((u64)count > ~(u64)offset) return nfserr_inval; if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0) return err; if (EX_ISSYNC(fhp->fh_export)) { if (file->f_op && file->f_op->fsync) { nfsd_sync(file); } else { err = nfserr_notsupp; } } nfsd_close(file); return err;}#endif /* CONFIG_NFSD_V3 *//* * Create a file (regular, directory, device, fifo); UNIX sockets * not yet implemented. * If the response fh has been verified, the parent directory should * already be locked. Note that the parent directory is left locked. * * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp */intnfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, char *fname, int flen, struct iattr *iap, int type, dev_t rdev, struct svc_fh *resfhp){ struct dentry *dentry, *dchild = NULL; struct inode *dirp; int err; err = nfserr_perm; if (!flen) goto out; err = nfserr_exist; if (isdotent(fname, flen)) goto out; err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE); if (err) goto out; dentry = fhp->fh_dentry; dirp = dentry->d_inode; err = nfserr_notdir; if(!dirp->i_op || !dirp->i_op->lookup) goto out; /* * Check whether the response file handle has been verified yet. * If it has, the parent directory should already be locked. */ if (!resfhp->fh_dentry) { /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */ fh_lock(fhp); dchild = lookup_one_len(fname, dentry, flen); err = PTR_ERR(dchild); if (IS_ERR(dchild)) goto out_nfserr; err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); if (err) goto out; } else { /* called from nfsd_proc_create */ dchild = dget(resfhp->fh_dentry); if (!fhp->fh_locked) { /* not actually possible */ printk(KERN_ERR "nfsd_create: parent %s/%s not locked!\n", dentry->d_parent->d_name.name, dentry->d_name.name); err = -EIO; goto out; } } /* * Make sure the child dentry is still negative ... */ err = nfserr_exist; if (dchild->d_inode) { dprintk("nfsd_create: dentry %s/%s not negative!\n", dentry->d_name.name, dchild->d_name.name); goto out; } if (!(iap->ia_valid & ATTR_MODE)) iap->ia_mode = 0; iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type; /* * Get the dir op function pointer. */ err = nfserr_perm; switch (type) { case S_IFREG: err = vfs_create(dirp, dchild, iap->ia_mode, NULL); break; case S_IFDIR: err = vfs_mkdir(dirp, dchild, iap->ia_mode); break; case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK: err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev); break; default: printk("nfsd: bad file type %o in nfsd_create\n", type); err = -EINVAL; } if (err < 0) goto out_nfserr; if (EX_ISSYNC(fhp->fh_export)) { nfsd_sync_dir(dentry); write_inode_now(dchild->d_inode, 1); } /* Set file attributes. Mode has already been set and * setting uid/gid works only for root. Irix appears to * send along the gid when it tries to implement setgid * directories via NFS. */ err = 0; if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0); /* * Update the file handle to get the new inode info. */ if (!err) err = fh_update(resfhp);out: if (dchild && !IS_ERR(dchild)) dput(dchild); return err;out_nfserr: err = nfserrno(err); goto out;}#ifdef CONFIG_NFSD_V3/* * NFSv3 version of nfsd_create */intnfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp, char *fname, int flen, struct iattr *iap, struct svc_fh *resfhp, int createmode, u32 *verifier, int *truncp){ struct dentry *dentry, *dchild = NULL; struct inode *dirp; int err; __u32 v_mtime=0, v_atime=0; int v_mode=0; err = nfserr_perm; if (!flen) goto out; err = nfserr_exist; if (isdotent(fname, flen)) goto out; if (!(iap->ia_valid & ATTR_MODE)) iap->ia_mode = 0; err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE); if (err) goto out; dentry = fhp->fh_dentry; dirp = dentry->d_inode; /* Get all the sanity checks out of the way before * we lock the parent. */ err = nfserr_notdir; if(!dirp->i_op || !dirp->i_op->lookup) goto out; fh_lock(fhp); /* * Compose the response file handle. */ dchild = lookup_one_len(fname, dentry, flen); err = PTR_ERR(dchild); if (IS_ERR(dchild)) goto out_nfserr; err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); if (err) goto out; if (createmode == NFS3_CREATE_EXCLUSIVE) { /* while the verifier would fit in mtime+atime, * solaris7 gets confused (bugid 4218508) if these have * the high bit set, so we use the mode as well
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?