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

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