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

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