📄 inode.c
字号:
/* * linux/fs/nfs/inode.c * * Copyright (C) 1992 Rick Sladkey * * nfs inode and superblock handling functions * * Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some * experimental NFS changes. Modularisation taken straight from SYS5 fs. * * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts. * J.S.Peatfield@damtp.cam.ac.uk * */#include <linux/config.h>#include <linux/module.h>#include <linux/init.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/mm.h>#include <linux/string.h>#include <linux/stat.h>#include <linux/errno.h>#include <linux/locks.h>#include <linux/unistd.h>#include <linux/sunrpc/clnt.h>#include <linux/sunrpc/stats.h>#include <linux/nfs_fs.h>#include <linux/nfs_mount.h>#include <linux/nfs_flushd.h>#include <linux/lockd/bind.h>#include <linux/smp_lock.h>#include <linux/seq_file.h>#include <asm/system.h>#include <asm/uaccess.h>#define CONFIG_NFS_SNAPSHOT 1#define NFSDBG_FACILITY NFSDBG_VFS#define NFS_PARANOIA 1static struct inode * __nfs_fhget(struct super_block *, struct nfs_fh *, struct nfs_fattr *);void nfs_zap_caches(struct inode *);static void nfs_invalidate_inode(struct inode *);static void nfs_read_inode(struct inode *);static void nfs_write_inode(struct inode *,int);static void nfs_delete_inode(struct inode *);static void nfs_put_super(struct super_block *);static void nfs_clear_inode(struct inode *);static void nfs_umount_begin(struct super_block *);static int nfs_statfs(struct super_block *, struct statfs *);static int nfs_show_options(struct seq_file *, struct vfsmount *);static struct super_operations nfs_sops = { read_inode: nfs_read_inode, write_inode: nfs_write_inode, delete_inode: nfs_delete_inode, put_super: nfs_put_super, statfs: nfs_statfs, clear_inode: nfs_clear_inode, umount_begin: nfs_umount_begin, show_options: nfs_show_options,};/* * RPC cruft for NFS */struct rpc_stat nfs_rpcstat = { &nfs_program };static struct rpc_version * nfs_version[] = { NULL, NULL, &nfs_version2,#ifdef CONFIG_NFS_V3 &nfs_version3,#endif};struct rpc_program nfs_program = { "nfs", NFS_PROGRAM, sizeof(nfs_version) / sizeof(nfs_version[0]), nfs_version, &nfs_rpcstat,};static inline unsigned longnfs_fattr_to_ino_t(struct nfs_fattr *fattr){ return nfs_fileid_to_ino_t(fattr->fileid);}/* * The "read_inode" function doesn't actually do anything: * the real data is filled in later in nfs_fhget. Here we * just mark the cache times invalid, and zero out i_mode * (the latter makes "nfs_refresh_inode" do the right thing * wrt pipe inodes) */static voidnfs_read_inode(struct inode * inode){ inode->i_blksize = inode->i_sb->s_blocksize; inode->i_mode = 0; inode->i_rdev = 0; /* We can't support UPDATE_ATIME(), since the server will reset it */ inode->i_flags |= S_NOATIME; INIT_LIST_HEAD(&inode->u.nfs_i.read); INIT_LIST_HEAD(&inode->u.nfs_i.dirty); INIT_LIST_HEAD(&inode->u.nfs_i.commit); INIT_LIST_HEAD(&inode->u.nfs_i.writeback); NFS_CACHEINV(inode); NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode); NFS_ATTRTIMEO_UPDATE(inode) = jiffies;}static voidnfs_write_inode(struct inode *inode, int sync){ int flags = sync ? FLUSH_WAIT : 0; nfs_sync_file(inode, NULL, 0, 0, flags);}static voidnfs_delete_inode(struct inode * inode){ dprintk("NFS: delete_inode(%x/%ld)\n", inode->i_dev, inode->i_ino); /* * The following can never actually happen... */ if (nfs_have_writebacks(inode) || nfs_have_read(inode)) { printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino); } clear_inode(inode);}/* * For the moment, the only task for the NFS clear_inode method is to * release the mmap credential */static voidnfs_clear_inode(struct inode *inode){ struct rpc_cred *cred = NFS_I(inode)->mm_cred; if (cred) put_rpccred(cred);}voidnfs_put_super(struct super_block *sb){ struct nfs_server *server = &sb->u.nfs_sb.s_server; struct rpc_clnt *rpc; /* * First get rid of the request flushing daemon. * Relies on rpc_shutdown_client() waiting on all * client tasks to finish. */ nfs_reqlist_exit(server); if ((rpc = server->client) != NULL) rpc_shutdown_client(rpc); nfs_reqlist_free(server); if (!(server->flags & NFS_MOUNT_NONLM)) lockd_down(); /* release rpc.lockd */ rpciod_down(); /* release rpciod */ kfree(server->hostname);}voidnfs_umount_begin(struct super_block *sb){ struct nfs_server *server = &sb->u.nfs_sb.s_server; struct rpc_clnt *rpc; /* -EIO all pending I/O */ if ((rpc = server->client) != NULL) rpc_killall_tasks(rpc);}static inline unsigned longnfs_block_bits(unsigned long bsize, unsigned char *nrbitsp){ /* make sure blocksize is a power of two */ if ((bsize & (bsize - 1)) || nrbitsp) { unsigned char nrbits; for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--) ; bsize = 1 << nrbits; if (nrbitsp) *nrbitsp = nrbits; } return bsize;}/* * Calculate the number of 512byte blocks used. */static inline unsigned longnfs_calc_block_size(u64 tsize){ loff_t used = (tsize + 511) >> 9; return (used > ULONG_MAX) ? ULONG_MAX : used;}/* * Compute and set NFS server blocksize */static inline unsigned longnfs_block_size(unsigned long bsize, unsigned char *nrbitsp){ if (bsize < 1024) bsize = NFS_DEF_FILE_IO_BUFFER_SIZE; else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE) bsize = NFS_MAX_FILE_IO_BUFFER_SIZE; return nfs_block_bits(bsize, nrbitsp);}/* * Obtain the root inode of the file system. */static struct inode *nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh){ struct nfs_server *server = &sb->u.nfs_sb.s_server; struct nfs_fattr fattr; struct inode *inode; int error; if ((error = server->rpc_ops->getroot(server, rootfh, &fattr)) < 0) { printk(KERN_NOTICE "nfs_get_root: getattr error = %d\n", -error); return NULL; } inode = __nfs_fhget(sb, rootfh, &fattr); return inode;}/* * The way this works is that the mount process passes a structure * in the data argument which contains the server's IP address * and the root file handle obtained from the server's mount * daemon. We stash these away in the private superblock fields. */struct super_block *nfs_read_super(struct super_block *sb, void *raw_data, int silent){ struct nfs_mount_data *data = (struct nfs_mount_data *) raw_data; struct nfs_server *server; struct rpc_xprt *xprt = NULL; struct rpc_clnt *clnt = NULL; struct nfs_fh *root = &data->root, fh; struct inode *root_inode = NULL; unsigned int authflavor; struct sockaddr_in srvaddr; struct rpc_timeout timeparms; struct nfs_fsinfo fsinfo; int tcp, version, maxlen; memset(&sb->u.nfs_sb, 0, sizeof(sb->u.nfs_sb)); if (!data) goto out_miss_args; memset(&fh, 0, sizeof(fh)); if (data->version != NFS_MOUNT_VERSION) { printk("nfs warning: mount version %s than kernel\n", data->version < NFS_MOUNT_VERSION ? "older" : "newer"); if (data->version < 2) data->namlen = 0; if (data->version < 3) data->bsize = 0; if (data->version < 4) { data->flags &= ~NFS_MOUNT_VER3; root = &fh; root->size = NFS2_FHSIZE; memcpy(root->data, data->old_root.data, NFS2_FHSIZE); } } /* We now require that the mount process passes the remote address */ memcpy(&srvaddr, &data->addr, sizeof(srvaddr)); if (srvaddr.sin_addr.s_addr == INADDR_ANY) goto out_no_remote; sb->s_magic = NFS_SUPER_MAGIC; sb->s_op = &nfs_sops; sb->s_blocksize_bits = 0; sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits); server = &sb->u.nfs_sb.s_server; server->rsize = nfs_block_size(data->rsize, NULL); server->wsize = nfs_block_size(data->wsize, NULL); server->flags = data->flags & NFS_MOUNT_FLAGMASK; if (data->flags & NFS_MOUNT_NOAC) { data->acregmin = data->acregmax = 0; data->acdirmin = data->acdirmax = 0; sb->s_flags |= MS_SYNCHRONOUS; } server->acregmin = data->acregmin*HZ; server->acregmax = data->acregmax*HZ; server->acdirmin = data->acdirmin*HZ; server->acdirmax = data->acdirmax*HZ; server->namelen = data->namlen; server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL); if (!server->hostname) goto out_unlock; strcpy(server->hostname, data->hostname); INIT_LIST_HEAD(&server->lru_read); INIT_LIST_HEAD(&server->lru_dirty); INIT_LIST_HEAD(&server->lru_commit); INIT_LIST_HEAD(&server->lru_busy); nfsv3_try_again: /* Check NFS protocol revision and initialize RPC op vector * and file handle pool. */ if (data->flags & NFS_MOUNT_VER3) {#ifdef CONFIG_NFS_V3 server->rpc_ops = &nfs_v3_clientops; version = 3; if (data->version < 4) { printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n"); goto out_unlock; }#else printk(KERN_NOTICE "NFS: NFSv3 not supported.\n"); goto out_unlock;#endif } else { server->rpc_ops = &nfs_v2_clientops; version = 2; } /* Which protocol do we use? */ tcp = (data->flags & NFS_MOUNT_TCP); /* Initialize timeout values */ timeparms.to_initval = data->timeo * HZ / 10; timeparms.to_retries = data->retrans; timeparms.to_maxval = tcp? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT; timeparms.to_exponential = 1; if (!timeparms.to_initval) timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10; if (!timeparms.to_retries) timeparms.to_retries = 5; /* Now create transport and client */ xprt = xprt_create_proto(tcp? IPPROTO_TCP : IPPROTO_UDP, &srvaddr, &timeparms); if (xprt == NULL) goto out_no_xprt; /* Choose authentication flavor */ authflavor = RPC_AUTH_UNIX; if (data->flags & NFS_MOUNT_SECURE) authflavor = RPC_AUTH_DES; else if (data->flags & NFS_MOUNT_KERBEROS) authflavor = RPC_AUTH_KRB; clnt = rpc_create_client(xprt, server->hostname, &nfs_program, version, authflavor); if (clnt == NULL) goto out_no_client; clnt->cl_intr = (data->flags & NFS_MOUNT_INTR)? 1 : 0; clnt->cl_softrtry = (data->flags & NFS_MOUNT_SOFT)? 1 : 0; clnt->cl_droppriv = (data->flags & NFS_MOUNT_BROKEN_SUID) ? 1 : 0; clnt->cl_chatty = 1; server->client = clnt; /* Fire up rpciod if not yet running */ if (rpciod_up() != 0) goto out_no_iod; /* * Keep the super block locked while we try to get * the root fh attributes. */ /* Did getting the root inode fail? */ if (!(root_inode = nfs_get_root(sb, root)) && (data->flags & NFS_MOUNT_VER3)) { data->flags &= ~NFS_MOUNT_VER3; rpciod_down();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -