root/fs/nfs/inode.c

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

DEFINITIONS

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

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