root/fs/nfs/inode.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. nfs_read_inode
  2. nfs_put_inode
  3. nfs_put_super
  4. nfs_read_super
  5. nfs_statfs
  6. nfs_fhget
  7. nfs_notify_change
  8. init_nfs_fs
  9. init_module
  10. cleanup_module

   1 /*
   2  *  linux/fs/nfs/inode.c
   3  *
   4  *  Copyright (C) 1992  Rick Sladkey
   5  *
   6  *  nfs inode and superblock handling functions
   7  *
   8  *  Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
   9  *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
  10  *
  11  *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
  12  *  J.S.Peatfield@damtp.cam.ac.uk
  13  *
  14  */
  15 
  16 #include <linux/module.h>
  17 
  18 #include <linux/sched.h>
  19 #include <linux/nfs_fs.h>
  20 #include <linux/kernel.h>
  21 #include <linux/mm.h>
  22 #include <linux/string.h>
  23 #include <linux/stat.h>
  24 #include <linux/errno.h>
  25 #include <linux/locks.h>
  26 
  27 #include <asm/system.h>
  28 #include <asm/segment.h>
  29 
  30 extern int close_fp(struct file *filp);
  31 
  32 static int nfs_notify_change(struct inode *, struct iattr *);
  33 static void nfs_put_inode(struct inode *);
  34 static void nfs_put_super(struct super_block *);
  35 static void nfs_read_inode(struct inode *);
  36 static void nfs_statfs(struct super_block *, struct statfs *, int bufsiz);
  37 
  38 static struct super_operations nfs_sops = { 
  39         nfs_read_inode,         /* read inode */
  40         nfs_notify_change,      /* notify change */
  41         NULL,                   /* write inode */
  42         nfs_put_inode,          /* put inode */
  43         nfs_put_super,          /* put superblock */
  44         NULL,                   /* write superblock */
  45         nfs_statfs,             /* stat filesystem */
  46         NULL
  47 };
  48 
  49 /*
  50  * The "read_inode" function doesn't actually do anything:
  51  * the real data is filled in later in nfs_fhget. Here we
  52  * just mark the cache times invalid, and zero out i_mode
  53  * (the latter makes "nfs_refresh_inode" do the right thing
  54  * wrt pipe inodes)
  55  */
  56 static void nfs_read_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  57 {
  58         inode->i_mode = 0;
  59         inode->i_op = NULL;
  60         NFS_CACHEINV(inode);
  61 }
  62 
  63 static void nfs_put_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65         if (inode->i_pipe)
  66                 clear_inode(inode);
  67 }
  68 
  69 void nfs_put_super(struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  70 {
  71         close_fp(sb->u.nfs_sb.s_server.file);
  72         rpc_closesock(sb->u.nfs_sb.s_server.rsock);
  73         lock_super(sb);
  74         sb->s_dev = 0;
  75         unlock_super(sb);
  76         MOD_DEC_USE_COUNT;
  77 }
  78 
  79 /*
  80  * The way this works is that the mount process passes a structure
  81  * in the data argument which contains an open socket to the NFS
  82  * server and the root file handle obtained from the server's mount
  83  * daemon.  We stash theses away in the private superblock fields.
  84  * Later we can add other mount parameters like caching values.
  85  */
  86 
  87 struct super_block *nfs_read_super(struct super_block *sb, void *raw_data,
     /* [previous][next][first][last][top][bottom][index][help] */
  88                                    int silent)
  89 {
  90         struct nfs_mount_data *data = (struct nfs_mount_data *) raw_data;
  91         struct nfs_server *server;
  92         unsigned int fd;
  93         struct file *filp;
  94 
  95         kdev_t dev = sb->s_dev;
  96 
  97         MOD_INC_USE_COUNT;
  98         if (!data) {
  99                 printk("nfs_read_super: missing data argument\n");
 100                 sb->s_dev = 0;
 101                 MOD_DEC_USE_COUNT;
 102                 return NULL;
 103         }
 104         fd = data->fd;
 105         if (data->version != NFS_MOUNT_VERSION) {
 106                 printk("nfs warning: mount version %s than kernel\n",
 107                         data->version < NFS_MOUNT_VERSION ? "older" : "newer");
 108         }
 109         if (fd >= NR_OPEN || !(filp = current->files->fd[fd])) {
 110                 printk("nfs_read_super: invalid file descriptor\n");
 111                 sb->s_dev = 0;
 112                 MOD_DEC_USE_COUNT;
 113                 return NULL;
 114         }
 115         if (!S_ISSOCK(filp->f_inode->i_mode)) {
 116                 printk("nfs_read_super: not a socket\n");
 117                 sb->s_dev = 0;
 118                 MOD_DEC_USE_COUNT;
 119                 return NULL;
 120         }
 121         filp->f_count++;
 122         lock_super(sb);
 123 
 124         sb->s_blocksize = 1024; /* XXX */
 125         sb->s_blocksize_bits = 10;
 126         sb->s_magic = NFS_SUPER_MAGIC;
 127         sb->s_dev = dev;
 128         sb->s_op = &nfs_sops;
 129         server = &sb->u.nfs_sb.s_server;
 130         server->file = filp;
 131         server->lock = 0;
 132         server->wait = NULL;
 133         server->flags = data->flags;
 134         server->rsize = data->rsize;
 135         if (server->rsize <= 0)
 136                 server->rsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
 137         else if (server->rsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
 138                 server->rsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
 139         server->wsize = data->wsize;
 140         if (server->wsize <= 0)
 141                 server->wsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
 142         else if (server->wsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
 143                 server->wsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
 144         server->timeo = data->timeo*HZ/10;
 145         server->retrans = data->retrans;
 146         server->acregmin = data->acregmin*HZ;
 147         server->acregmax = data->acregmax*HZ;
 148         server->acdirmin = data->acdirmin*HZ;
 149         server->acdirmax = data->acdirmax*HZ;
 150         strcpy(server->hostname, data->hostname);
 151 
 152         /* Start of JSP NFS patch */
 153         /* Check if passed address in data->addr */
 154         if (data->addr.sin_addr.s_addr == INADDR_ANY) {  /* No address passed */
 155           if (((struct sockaddr_in *)(&server->toaddr))->sin_addr.s_addr == INADDR_ANY) {
 156             printk("NFS: Error passed unconnected socket and no address\n") ;
 157             MOD_DEC_USE_COUNT;
 158             return NULL ;
 159           } else {
 160             /* Need access to socket internals  JSP */
 161             struct socket *sock;
 162             int dummylen ;
 163 
 164          /*   printk("NFS: using socket address\n") ;*/
 165 
 166             sock = &((filp->f_inode)->u.socket_i);
 167 
 168             /* extract the other end of the socket into server->toaddr */
 169             sock->ops->getname(sock, &(server->toaddr), &dummylen, 1) ;
 170           }
 171         } else {
 172         /*  printk("NFS: coppying passed addr to server->toaddr\n") ;*/
 173           memcpy((char *)&(server->toaddr),(char *)(&data->addr),sizeof(server->toaddr));
 174         }
 175         /* End of JSP NFS patch */
 176 
 177         if ((server->rsock = rpc_makesock(filp)) == NULL) {
 178                 printk("NFS: cannot create RPC socket.\n");
 179                 MOD_DEC_USE_COUNT;
 180                 return NULL;
 181         }
 182 
 183         sb->u.nfs_sb.s_root = data->root;
 184         unlock_super(sb);
 185         if (!(sb->s_mounted = nfs_fhget(sb, &data->root, NULL))) {
 186                 sb->s_dev = 0;
 187                 printk("nfs_read_super: get root inode failed\n");
 188                 MOD_DEC_USE_COUNT;
 189                 return NULL;
 190         }
 191         return sb;
 192 }
 193 
 194 void nfs_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196         int error;
 197         struct nfs_fsinfo res;
 198         struct statfs tmp;
 199 
 200         error = nfs_proc_statfs(&sb->u.nfs_sb.s_server, &sb->u.nfs_sb.s_root,
 201                 &res);
 202         if (error) {
 203                 printk("nfs_statfs: statfs error = %d\n", -error);
 204                 res.bsize = res.blocks = res.bfree = res.bavail = 0;
 205         }
 206         tmp.f_type = NFS_SUPER_MAGIC;
 207         tmp.f_bsize = res.bsize;
 208         tmp.f_blocks = res.blocks;
 209         tmp.f_bfree = res.bfree;
 210         tmp.f_bavail = res.bavail;
 211         tmp.f_files = 0;
 212         tmp.f_ffree = 0;
 213         tmp.f_namelen = NAME_MAX;
 214         memcpy_tofs(buf, &tmp, bufsiz);
 215 }
 216 
 217 /*
 218  * This is our own version of iget that looks up inodes by file handle
 219  * instead of inode number.  We use this technique instead of using
 220  * the vfs read_inode function because there is no way to pass the
 221  * file handle or current attributes into the read_inode function.
 222  * We just have to be careful not to subvert iget's special handling
 223  * of mount points.
 224  */
 225 
 226 struct inode *nfs_fhget(struct super_block *sb, struct nfs_fh *fhandle,
     /* [previous][next][first][last][top][bottom][index][help] */
 227                         struct nfs_fattr *fattr)
 228 {
 229         struct nfs_fattr newfattr;
 230         int error;
 231         struct inode *inode;
 232 
 233         if (!sb) {
 234                 printk("nfs_fhget: super block is NULL\n");
 235                 return NULL;
 236         }
 237         if (!fattr) {
 238                 error = nfs_proc_getattr(&sb->u.nfs_sb.s_server, fhandle,
 239                         &newfattr);
 240                 if (error) {
 241                         printk("nfs_fhget: getattr error = %d\n", -error);
 242                         return NULL;
 243                 }
 244                 fattr = &newfattr;
 245         }
 246         if (!(inode = iget(sb, fattr->fileid))) {
 247                 printk("nfs_fhget: iget failed\n");
 248                 return NULL;
 249         }
 250         if (inode->i_dev == sb->s_dev) {
 251                 if (inode->i_ino != fattr->fileid) {
 252                         printk("nfs_fhget: unexpected inode from iget\n");
 253                         return inode;
 254                 }
 255                 *NFS_FH(inode) = *fhandle;
 256                 nfs_refresh_inode(inode, fattr);
 257         }
 258         return inode;
 259 }
 260 
 261 int nfs_notify_change(struct inode *inode, struct iattr *attr)
     /* [previous][next][first][last][top][bottom][index][help] */
 262 {
 263         struct nfs_sattr sattr;
 264         struct nfs_fattr fattr;
 265         int error;
 266 
 267         sattr.mode = (unsigned) -1;
 268         if (attr->ia_valid & ATTR_MODE) 
 269                 sattr.mode = attr->ia_mode;
 270 
 271         sattr.uid = (unsigned) -1;
 272         if (attr->ia_valid & ATTR_UID)
 273                 sattr.uid = attr->ia_uid;
 274 
 275         sattr.gid = (unsigned) -1;
 276         if (attr->ia_valid & ATTR_GID)
 277                 sattr.gid = attr->ia_gid;
 278 
 279 
 280         sattr.size = (unsigned) -1;
 281         if (attr->ia_valid & ATTR_SIZE)
 282                 sattr.size = S_ISREG(inode->i_mode) ? attr->ia_size : -1;
 283 
 284         sattr.mtime.seconds = sattr.mtime.useconds = (unsigned) -1;
 285         if (attr->ia_valid & ATTR_MTIME) {
 286                 sattr.mtime.seconds = attr->ia_mtime;
 287                 sattr.mtime.useconds = 0;
 288         }
 289 
 290         sattr.atime.seconds = sattr.atime.useconds = (unsigned) -1;
 291         if (attr->ia_valid & ATTR_ATIME) {
 292                 sattr.atime.seconds = attr->ia_atime;
 293                 sattr.atime.useconds = 0;
 294         }
 295 
 296         error = nfs_proc_setattr(NFS_SERVER(inode), NFS_FH(inode),
 297                 &sattr, &fattr);
 298         if (!error)
 299                 nfs_refresh_inode(inode, &fattr);
 300         inode->i_dirt = 0;
 301         return error;
 302 }
 303 
 304 /* Every kernel module contains stuff like this. */
 305 
 306 static struct file_system_type nfs_fs_type = {
 307         nfs_read_super, "nfs", 0, NULL
 308 };
 309 
 310 int init_nfs_fs(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 311 {
 312         return register_filesystem(&nfs_fs_type);
 313 }
 314 
 315 #ifdef MODULE
 316 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 317 {
 318         int status;
 319 
 320         if ((status = init_nfs_fs()) == 0)
 321                 register_symtab(0);
 322         return status;
 323 }
 324 
 325 void cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 326 {
 327         unregister_filesystem(&nfs_fs_type);
 328         nfs_kfree_cache();
 329 }
 330 
 331 #endif

/* [previous][next][first][last][top][bottom][index][help] */