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

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