b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 1) // SPDX-License-Identifier: GPL-2.0
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) * fs/isofs/export.c
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 5) * (C) 2004 Paul Serice - The new inode scheme requires switching
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 6) * from iget() to iget5_locked() which means
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 7) * the NFS export operations have to be hand
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 8) * coded because the default routines rely on
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 9) * iget().
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 10) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 11) * The following files are helpful:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 12) *
ec23eb54fbc7a (Mauro Carvalho Chehab 2019-07-26 09:51:27 -0300 13) * Documentation/filesystems/nfs/exporting.rst
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 14) * fs/exportfs/expfs.c.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 15) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 16)
94f2f715771d0 (Al Viro 2005-04-25 18:32:12 -0700 17) #include "isofs.h"
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 18)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 19) static struct dentry *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 20) isofs_export_iget(struct super_block *sb,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 21) unsigned long block,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 22) unsigned long offset,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 23) __u32 generation)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 24) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 25) struct inode *inode;
440037287c5eb (Christoph Hellwig 2008-08-11 15:49:04 +0200 26)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 27) if (block == 0)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 28) return ERR_PTR(-ESTALE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 29) inode = isofs_iget(sb, block, offset);
c4386c83bf849 (David Howells 2008-02-07 00:15:41 -0800 30) if (IS_ERR(inode))
c4386c83bf849 (David Howells 2008-02-07 00:15:41 -0800 31) return ERR_CAST(inode);
c4386c83bf849 (David Howells 2008-02-07 00:15:41 -0800 32) if (generation && inode->i_generation != generation) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 33) iput(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 34) return ERR_PTR(-ESTALE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 35) }
440037287c5eb (Christoph Hellwig 2008-08-11 15:49:04 +0200 36) return d_obtain_alias(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 37) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 38)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 39) /* This function is surprisingly simple. The trick is understanding
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 40) * that "child" is always a directory. So, to find its parent, you
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 41) * simply need to find its ".." entry, normalize its block and offset,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 42) * and return the underlying inode. See the comments for
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 43) * isofs_normalize_block_and_offset(). */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 44) static struct dentry *isofs_export_get_parent(struct dentry *child)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 45) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 46) unsigned long parent_block = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 47) unsigned long parent_offset = 0;
2b0143b5c986b (David Howells 2015-03-17 22:25:59 +0000 48) struct inode *child_inode = d_inode(child);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 49) struct iso_inode_info *e_child_inode = ISOFS_I(child_inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 50) struct iso_directory_record *de = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 51) struct buffer_head * bh = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 52) struct dentry *rv = NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 53)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 54) /* "child" must always be a directory. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 55) if (!S_ISDIR(child_inode->i_mode)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 56) printk(KERN_ERR "isofs: isofs_export_get_parent(): "
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 57) "child is not a directory!\n");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 58) rv = ERR_PTR(-EACCES);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 59) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 60) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 61)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 62) /* It is an invariant that the directory offset is zero. If
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 63) * it is not zero, it means the directory failed to be
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 64) * normalized for some reason. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 65) if (e_child_inode->i_iget5_offset != 0) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 66) printk(KERN_ERR "isofs: isofs_export_get_parent(): "
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 67) "child directory not normalized!\n");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 68) rv = ERR_PTR(-EACCES);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 69) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 70) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 71)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 72) /* The child inode has been normalized such that its
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 73) * i_iget5_block value points to the "." entry. Fortunately,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 74) * the ".." entry is located in the same block. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 75) parent_block = e_child_inode->i_iget5_block;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 76)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 77) /* Get the block in question. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 78) bh = sb_bread(child_inode->i_sb, parent_block);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 79) if (bh == NULL) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 80) rv = ERR_PTR(-EACCES);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 81) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 82) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 83)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 84) /* This is the "." entry. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 85) de = (struct iso_directory_record*)bh->b_data;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 86)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 87) /* The ".." entry is always the second entry. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 88) parent_offset = (unsigned long)isonum_711(de->length);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 89) de = (struct iso_directory_record*)(bh->b_data + parent_offset);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 90)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 91) /* Verify it is in fact the ".." entry. */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 92) if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 93) printk(KERN_ERR "isofs: Unable to find the \"..\" "
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 94) "directory for NFS.\n");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 95) rv = ERR_PTR(-EACCES);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 96) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 97) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 98)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 99) /* Normalize */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 100) isofs_normalize_block_and_offset(de, &parent_block, &parent_offset);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 101)
440037287c5eb (Christoph Hellwig 2008-08-11 15:49:04 +0200 102) rv = d_obtain_alias(isofs_iget(child_inode->i_sb, parent_block,
440037287c5eb (Christoph Hellwig 2008-08-11 15:49:04 +0200 103) parent_offset));
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 104) out:
440037287c5eb (Christoph Hellwig 2008-08-11 15:49:04 +0200 105) if (bh)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 106) brelse(bh);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 107) return rv;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 108) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 109)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 110) static int
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 111) isofs_export_encode_fh(struct inode *inode,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 112) __u32 *fh32,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 113) int *max_len,
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 114) struct inode *parent)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 115) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 116) struct iso_inode_info * ei = ISOFS_I(inode);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 117) int len = *max_len;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 118) int type = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 119) __u16 *fh16 = (__u16*)fh32;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 120)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 121) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 122) * WARNING: max_len is 5 for NFSv2. Because of this
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 123) * limitation, we use the lower 16 bits of fh32[1] to hold the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 124) * offset of the inode and the upper 16 bits of fh32[1] to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 125) * hold the offset of the parent.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 126) */
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 127) if (parent && (len < 5)) {
5fe0c2378884e (Aneesh Kumar K.V 2011-01-29 18:43:25 +0530 128) *max_len = 5;
94e07a7590ae8 (Namjae Jeon 2013-02-17 15:48:11 +0900 129) return FILEID_INVALID;
5fe0c2378884e (Aneesh Kumar K.V 2011-01-29 18:43:25 +0530 130) } else if (len < 3) {
5fe0c2378884e (Aneesh Kumar K.V 2011-01-29 18:43:25 +0530 131) *max_len = 3;
94e07a7590ae8 (Namjae Jeon 2013-02-17 15:48:11 +0900 132) return FILEID_INVALID;
5fe0c2378884e (Aneesh Kumar K.V 2011-01-29 18:43:25 +0530 133) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 134)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 135) len = 3;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 136) fh32[0] = ei->i_iget5_block;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 137) fh16[2] = (__u16)ei->i_iget5_offset; /* fh16 [sic] */
fe685aabf7c8c (Mathias Krause 2012-07-12 08:46:54 +0200 138) fh16[3] = 0; /* avoid leaking uninitialized data */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 139) fh32[2] = inode->i_generation;
b0b0382bb4904 (Al Viro 2012-04-02 14:34:06 -0400 140) if (parent) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 141) struct iso_inode_info *eparent;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 142) eparent = ISOFS_I(parent);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 143) fh32[3] = eparent->i_iget5_block;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 144) fh16[3] = (__u16)eparent->i_iget5_offset; /* fh16 [sic] */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 145) fh32[4] = parent->i_generation;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 146) len = 5;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 147) type = 2;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 148) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 149) *max_len = len;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 150) return type;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 151) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 152)
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 153) struct isofs_fid {
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 154) u32 block;
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 155) u16 offset;
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 156) u16 parent_offset;
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 157) u32 generation;
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 158) u32 parent_block;
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 159) u32 parent_generation;
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 160) };
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 161)
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 162) static struct dentry *isofs_fh_to_dentry(struct super_block *sb,
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 163) struct fid *fid, int fh_len, int fh_type)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 164) {
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 165) struct isofs_fid *ifid = (struct isofs_fid *)fid;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 166)
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 167) if (fh_len < 3 || fh_type > 2)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 168) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 169)
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 170) return isofs_export_iget(sb, ifid->block, ifid->offset,
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 171) ifid->generation);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 172) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 173)
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 174) static struct dentry *isofs_fh_to_parent(struct super_block *sb,
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 175) struct fid *fid, int fh_len, int fh_type)
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 176) {
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 177) struct isofs_fid *ifid = (struct isofs_fid *)fid;
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 178)
35c2a7f4908d4 (Hugh Dickins 2012-10-07 20:32:51 -0700 179) if (fh_len < 2 || fh_type != 2)
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 180) return NULL;
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 181)
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 182) return isofs_export_iget(sb,
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 183) fh_len > 2 ? ifid->parent_block : 0,
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 184) ifid->parent_offset,
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 185) fh_len > 4 ? ifid->parent_generation : 0);
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 186) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 187)
3965516440594 (Christoph Hellwig 2007-10-21 16:42:17 -0700 188) const struct export_operations isofs_export_ops = {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 189) .encode_fh = isofs_export_encode_fh,
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 190) .fh_to_dentry = isofs_fh_to_dentry,
905251a02eecc (Christoph Hellwig 2007-10-21 16:42:12 -0700 191) .fh_to_parent = isofs_fh_to_parent,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 192) .get_parent = isofs_export_get_parent,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 193) };