Linux Audio

Check our new training course

Loading...
v3.15
 
  1/* fs/fat/nfs.c
  2 *
  3 * This software is licensed under the terms of the GNU General Public
  4 * License version 2, as published by the Free Software Foundation, and
  5 * may be copied, distributed, and modified under those terms.
  6 *
  7 * This program is distributed in the hope that it will be useful,
  8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 10 * GNU General Public License for more details.
 11 *
 12 */
 13
 14#include <linux/exportfs.h>
 15#include "fat.h"
 16
 17struct fat_fid {
 18	u32 i_gen;
 19	u32 i_pos_low;
 20	u16 i_pos_hi;
 21	u16 parent_i_pos_hi;
 22	u32 parent_i_pos_low;
 23	u32 parent_i_gen;
 24};
 25
 26#define FAT_FID_SIZE_WITHOUT_PARENT 3
 27#define FAT_FID_SIZE_WITH_PARENT (sizeof(struct fat_fid)/sizeof(u32))
 28
 29/**
 30 * Look up a directory inode given its starting cluster.
 31 */
 32static struct inode *fat_dget(struct super_block *sb, int i_logstart)
 33{
 34	struct msdos_sb_info *sbi = MSDOS_SB(sb);
 35	struct hlist_head *head;
 36	struct msdos_inode_info *i;
 37	struct inode *inode = NULL;
 38
 39	head = sbi->dir_hashtable + fat_dir_hash(i_logstart);
 40	spin_lock(&sbi->dir_hash_lock);
 41	hlist_for_each_entry(i, head, i_dir_hash) {
 42		BUG_ON(i->vfs_inode.i_sb != sb);
 43		if (i->i_logstart != i_logstart)
 44			continue;
 45		inode = igrab(&i->vfs_inode);
 46		if (inode)
 47			break;
 48	}
 49	spin_unlock(&sbi->dir_hash_lock);
 50	return inode;
 51}
 52
 53static struct inode *fat_ilookup(struct super_block *sb, u64 ino, loff_t i_pos)
 54{
 55	if (MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO)
 56		return fat_iget(sb, i_pos);
 57
 58	else {
 59		if ((ino < MSDOS_ROOT_INO) || (ino == MSDOS_FSINFO_INO))
 60			return NULL;
 61		return ilookup(sb, ino);
 62	}
 63}
 64
 65static struct inode *__fat_nfs_get_inode(struct super_block *sb,
 66				       u64 ino, u32 generation, loff_t i_pos)
 67{
 68	struct inode *inode = fat_ilookup(sb, ino, i_pos);
 69
 70	if (inode && generation && (inode->i_generation != generation)) {
 71		iput(inode);
 72		inode = NULL;
 73	}
 74	if (inode == NULL && MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
 75		struct buffer_head *bh = NULL;
 76		struct msdos_dir_entry *de ;
 77		sector_t blocknr;
 78		int offset;
 79		fat_get_blknr_offset(MSDOS_SB(sb), i_pos, &blocknr, &offset);
 80		bh = sb_bread(sb, blocknr);
 81		if (!bh) {
 82			fat_msg(sb, KERN_ERR,
 83				"unable to read block(%llu) for building NFS inode",
 84				(llu)blocknr);
 85			return inode;
 86		}
 87		de = (struct msdos_dir_entry *)bh->b_data;
 88		/* If a file is deleted on server and client is not updated
 89		 * yet, we must not build the inode upon a lookup call.
 90		 */
 91		if (IS_FREE(de[offset].name))
 92			inode = NULL;
 93		else
 94			inode = fat_build_inode(sb, &de[offset], i_pos);
 95		brelse(bh);
 96	}
 97
 98	return inode;
 99}
100
101static struct inode *fat_nfs_get_inode(struct super_block *sb,
102				       u64 ino, u32 generation)
103{
104
105	return __fat_nfs_get_inode(sb, ino, generation, 0);
106}
107
108static int
109fat_encode_fh_nostale(struct inode *inode, __u32 *fh, int *lenp,
110		      struct inode *parent)
111{
112	int len = *lenp;
113	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
114	struct fat_fid *fid = (struct fat_fid *) fh;
115	loff_t i_pos;
116	int type = FILEID_FAT_WITHOUT_PARENT;
117
118	if (parent) {
119		if (len < FAT_FID_SIZE_WITH_PARENT) {
120			*lenp = FAT_FID_SIZE_WITH_PARENT;
121			return FILEID_INVALID;
122		}
123	} else {
124		if (len < FAT_FID_SIZE_WITHOUT_PARENT) {
125			*lenp = FAT_FID_SIZE_WITHOUT_PARENT;
126			return FILEID_INVALID;
127		}
128	}
129
130	i_pos = fat_i_pos_read(sbi, inode);
131	*lenp = FAT_FID_SIZE_WITHOUT_PARENT;
132	fid->i_gen = inode->i_generation;
133	fid->i_pos_low = i_pos & 0xFFFFFFFF;
134	fid->i_pos_hi = (i_pos >> 32) & 0xFFFF;
135	if (parent) {
136		i_pos = fat_i_pos_read(sbi, parent);
137		fid->parent_i_pos_hi = (i_pos >> 32) & 0xFFFF;
138		fid->parent_i_pos_low = i_pos & 0xFFFFFFFF;
139		fid->parent_i_gen = parent->i_generation;
140		type = FILEID_FAT_WITH_PARENT;
141		*lenp = FAT_FID_SIZE_WITH_PARENT;
 
 
 
 
 
 
142	}
143
144	return type;
145}
146
147/**
148 * Map a NFS file handle to a corresponding dentry.
149 * The dentry may or may not be connected to the filesystem root.
150 */
151static struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fid,
152				int fh_len, int fh_type)
153{
154	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
155				    fat_nfs_get_inode);
156}
157
158static struct dentry *fat_fh_to_dentry_nostale(struct super_block *sb,
159					       struct fid *fh, int fh_len,
160					       int fh_type)
161{
162	struct inode *inode = NULL;
163	struct fat_fid *fid = (struct fat_fid *)fh;
164	loff_t i_pos;
165
166	switch (fh_type) {
167	case FILEID_FAT_WITHOUT_PARENT:
168		if (fh_len < FAT_FID_SIZE_WITHOUT_PARENT)
169			return NULL;
170		break;
171	case FILEID_FAT_WITH_PARENT:
172		if (fh_len < FAT_FID_SIZE_WITH_PARENT)
173			return NULL;
174		break;
175	default:
176		return NULL;
177	}
178	i_pos = fid->i_pos_hi;
179	i_pos = (i_pos << 32) | (fid->i_pos_low);
180	inode = __fat_nfs_get_inode(sb, 0, fid->i_gen, i_pos);
181
182	return d_obtain_alias(inode);
183}
184
185/*
186 * Find the parent for a file specified by NFS handle.
187 * This requires that the handle contain the i_ino of the parent.
188 */
189static struct dentry *fat_fh_to_parent(struct super_block *sb, struct fid *fid,
190				int fh_len, int fh_type)
191{
192	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
193				    fat_nfs_get_inode);
194}
195
196static struct dentry *fat_fh_to_parent_nostale(struct super_block *sb,
197					       struct fid *fh, int fh_len,
198					       int fh_type)
199{
200	struct inode *inode = NULL;
201	struct fat_fid *fid = (struct fat_fid *)fh;
202	loff_t i_pos;
203
204	if (fh_len < FAT_FID_SIZE_WITH_PARENT)
205		return NULL;
206
207	switch (fh_type) {
208	case FILEID_FAT_WITH_PARENT:
209		i_pos = fid->parent_i_pos_hi;
210		i_pos = (i_pos << 32) | (fid->parent_i_pos_low);
211		inode = __fat_nfs_get_inode(sb, 0, fid->parent_i_gen, i_pos);
212		break;
213	}
214
215	return d_obtain_alias(inode);
216}
217
218/*
219 * Rebuild the parent for a directory that is not connected
220 *  to the filesystem root
221 */
222static
223struct inode *fat_rebuild_parent(struct super_block *sb, int parent_logstart)
224{
225	int search_clus, clus_to_match;
226	struct msdos_dir_entry *de;
227	struct inode *parent = NULL;
228	struct inode *dummy_grand_parent = NULL;
229	struct fat_slot_info sinfo;
230	struct msdos_sb_info *sbi = MSDOS_SB(sb);
231	sector_t blknr = fat_clus_to_blknr(sbi, parent_logstart);
232	struct buffer_head *parent_bh = sb_bread(sb, blknr);
233	if (!parent_bh) {
234		fat_msg(sb, KERN_ERR,
235			"unable to read cluster of parent directory");
236		return NULL;
237	}
238
239	de = (struct msdos_dir_entry *) parent_bh->b_data;
240	clus_to_match = fat_get_start(sbi, &de[0]);
241	search_clus = fat_get_start(sbi, &de[1]);
242
243	dummy_grand_parent = fat_dget(sb, search_clus);
244	if (!dummy_grand_parent) {
245		dummy_grand_parent = new_inode(sb);
246		if (!dummy_grand_parent) {
247			brelse(parent_bh);
248			return parent;
249		}
250
251		dummy_grand_parent->i_ino = iunique(sb, MSDOS_ROOT_INO);
252		fat_fill_inode(dummy_grand_parent, &de[1]);
253		MSDOS_I(dummy_grand_parent)->i_pos = -1;
254	}
255
256	if (!fat_scan_logstart(dummy_grand_parent, clus_to_match, &sinfo))
257		parent = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
258
259	brelse(parent_bh);
260	iput(dummy_grand_parent);
261
262	return parent;
263}
264
265/*
266 * Find the parent for a directory that is not currently connected to
267 * the filesystem root.
268 *
269 * On entry, the caller holds child_dir->d_inode->i_mutex.
270 */
271static struct dentry *fat_get_parent(struct dentry *child_dir)
272{
273	struct super_block *sb = child_dir->d_sb;
274	struct buffer_head *bh = NULL;
275	struct msdos_dir_entry *de;
276	struct inode *parent_inode = NULL;
277	struct msdos_sb_info *sbi = MSDOS_SB(sb);
278
279	if (!fat_get_dotdot_entry(child_dir->d_inode, &bh, &de)) {
280		int parent_logstart = fat_get_start(sbi, de);
281		parent_inode = fat_dget(sb, parent_logstart);
282		if (!parent_inode && sbi->options.nfs == FAT_NFS_NOSTALE_RO)
283			parent_inode = fat_rebuild_parent(sb, parent_logstart);
284	}
285	brelse(bh);
286
287	return d_obtain_alias(parent_inode);
288}
289
290const struct export_operations fat_export_ops = {
 
291	.fh_to_dentry   = fat_fh_to_dentry,
292	.fh_to_parent   = fat_fh_to_parent,
293	.get_parent     = fat_get_parent,
294};
295
296const struct export_operations fat_export_ops_nostale = {
297	.encode_fh      = fat_encode_fh_nostale,
298	.fh_to_dentry   = fat_fh_to_dentry_nostale,
299	.fh_to_parent   = fat_fh_to_parent_nostale,
300	.get_parent     = fat_get_parent,
301};
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* fs/fat/nfs.c
 
 
 
 
 
 
 
 
 
 
  3 */
  4
  5#include <linux/exportfs.h>
  6#include "fat.h"
  7
  8struct fat_fid {
  9	u32 i_gen;
 10	u32 i_pos_low;
 11	u16 i_pos_hi;
 12	u16 parent_i_pos_hi;
 13	u32 parent_i_pos_low;
 14	u32 parent_i_gen;
 15};
 16
 17#define FAT_FID_SIZE_WITHOUT_PARENT 3
 18#define FAT_FID_SIZE_WITH_PARENT (sizeof(struct fat_fid)/sizeof(u32))
 19
 20/*
 21 * Look up a directory inode given its starting cluster.
 22 */
 23static struct inode *fat_dget(struct super_block *sb, int i_logstart)
 24{
 25	struct msdos_sb_info *sbi = MSDOS_SB(sb);
 26	struct hlist_head *head;
 27	struct msdos_inode_info *i;
 28	struct inode *inode = NULL;
 29
 30	head = sbi->dir_hashtable + fat_dir_hash(i_logstart);
 31	spin_lock(&sbi->dir_hash_lock);
 32	hlist_for_each_entry(i, head, i_dir_hash) {
 33		BUG_ON(i->vfs_inode.i_sb != sb);
 34		if (i->i_logstart != i_logstart)
 35			continue;
 36		inode = igrab(&i->vfs_inode);
 37		if (inode)
 38			break;
 39	}
 40	spin_unlock(&sbi->dir_hash_lock);
 41	return inode;
 42}
 43
 44static struct inode *fat_ilookup(struct super_block *sb, u64 ino, loff_t i_pos)
 45{
 46	if (MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO)
 47		return fat_iget(sb, i_pos);
 48
 49	else {
 50		if ((ino < MSDOS_ROOT_INO) || (ino == MSDOS_FSINFO_INO))
 51			return NULL;
 52		return ilookup(sb, ino);
 53	}
 54}
 55
 56static struct inode *__fat_nfs_get_inode(struct super_block *sb,
 57				       u64 ino, u32 generation, loff_t i_pos)
 58{
 59	struct inode *inode = fat_ilookup(sb, ino, i_pos);
 60
 61	if (inode && generation && (inode->i_generation != generation)) {
 62		iput(inode);
 63		inode = NULL;
 64	}
 65	if (inode == NULL && MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
 66		struct buffer_head *bh = NULL;
 67		struct msdos_dir_entry *de ;
 68		sector_t blocknr;
 69		int offset;
 70		fat_get_blknr_offset(MSDOS_SB(sb), i_pos, &blocknr, &offset);
 71		bh = sb_bread(sb, blocknr);
 72		if (!bh) {
 73			fat_msg(sb, KERN_ERR,
 74				"unable to read block(%llu) for building NFS inode",
 75				(llu)blocknr);
 76			return inode;
 77		}
 78		de = (struct msdos_dir_entry *)bh->b_data;
 79		/* If a file is deleted on server and client is not updated
 80		 * yet, we must not build the inode upon a lookup call.
 81		 */
 82		if (IS_FREE(de[offset].name))
 83			inode = NULL;
 84		else
 85			inode = fat_build_inode(sb, &de[offset], i_pos);
 86		brelse(bh);
 87	}
 88
 89	return inode;
 90}
 91
 92static struct inode *fat_nfs_get_inode(struct super_block *sb,
 93				       u64 ino, u32 generation)
 94{
 95
 96	return __fat_nfs_get_inode(sb, ino, generation, 0);
 97}
 98
 99static int
100fat_encode_fh_nostale(struct inode *inode, __u32 *fh, int *lenp,
101		      struct inode *parent)
102{
103	int len = *lenp;
104	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
105	struct fat_fid *fid = (struct fat_fid *) fh;
106	loff_t i_pos;
107	int type = FILEID_FAT_WITHOUT_PARENT;
108
109	if (parent) {
110		if (len < FAT_FID_SIZE_WITH_PARENT) {
111			*lenp = FAT_FID_SIZE_WITH_PARENT;
112			return FILEID_INVALID;
113		}
114	} else {
115		if (len < FAT_FID_SIZE_WITHOUT_PARENT) {
116			*lenp = FAT_FID_SIZE_WITHOUT_PARENT;
117			return FILEID_INVALID;
118		}
119	}
120
121	i_pos = fat_i_pos_read(sbi, inode);
122	*lenp = FAT_FID_SIZE_WITHOUT_PARENT;
123	fid->i_gen = inode->i_generation;
124	fid->i_pos_low = i_pos & 0xFFFFFFFF;
125	fid->i_pos_hi = (i_pos >> 32) & 0xFFFF;
126	if (parent) {
127		i_pos = fat_i_pos_read(sbi, parent);
128		fid->parent_i_pos_hi = (i_pos >> 32) & 0xFFFF;
129		fid->parent_i_pos_low = i_pos & 0xFFFFFFFF;
130		fid->parent_i_gen = parent->i_generation;
131		type = FILEID_FAT_WITH_PARENT;
132		*lenp = FAT_FID_SIZE_WITH_PARENT;
133	} else {
134		/*
135		 * We need to initialize this field because the fh is actually
136		 * 12 bytes long
137		 */
138		fid->parent_i_pos_hi = 0;
139	}
140
141	return type;
142}
143
144/*
145 * Map a NFS file handle to a corresponding dentry.
146 * The dentry may or may not be connected to the filesystem root.
147 */
148static struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fid,
149				int fh_len, int fh_type)
150{
151	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
152				    fat_nfs_get_inode);
153}
154
155static struct dentry *fat_fh_to_dentry_nostale(struct super_block *sb,
156					       struct fid *fh, int fh_len,
157					       int fh_type)
158{
159	struct inode *inode = NULL;
160	struct fat_fid *fid = (struct fat_fid *)fh;
161	loff_t i_pos;
162
163	switch (fh_type) {
164	case FILEID_FAT_WITHOUT_PARENT:
165		if (fh_len < FAT_FID_SIZE_WITHOUT_PARENT)
166			return NULL;
167		break;
168	case FILEID_FAT_WITH_PARENT:
169		if (fh_len < FAT_FID_SIZE_WITH_PARENT)
170			return NULL;
171		break;
172	default:
173		return NULL;
174	}
175	i_pos = fid->i_pos_hi;
176	i_pos = (i_pos << 32) | (fid->i_pos_low);
177	inode = __fat_nfs_get_inode(sb, 0, fid->i_gen, i_pos);
178
179	return d_obtain_alias(inode);
180}
181
182/*
183 * Find the parent for a file specified by NFS handle.
184 * This requires that the handle contain the i_ino of the parent.
185 */
186static struct dentry *fat_fh_to_parent(struct super_block *sb, struct fid *fid,
187				int fh_len, int fh_type)
188{
189	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
190				    fat_nfs_get_inode);
191}
192
193static struct dentry *fat_fh_to_parent_nostale(struct super_block *sb,
194					       struct fid *fh, int fh_len,
195					       int fh_type)
196{
197	struct inode *inode = NULL;
198	struct fat_fid *fid = (struct fat_fid *)fh;
199	loff_t i_pos;
200
201	if (fh_len < FAT_FID_SIZE_WITH_PARENT)
202		return NULL;
203
204	switch (fh_type) {
205	case FILEID_FAT_WITH_PARENT:
206		i_pos = fid->parent_i_pos_hi;
207		i_pos = (i_pos << 32) | (fid->parent_i_pos_low);
208		inode = __fat_nfs_get_inode(sb, 0, fid->parent_i_gen, i_pos);
209		break;
210	}
211
212	return d_obtain_alias(inode);
213}
214
215/*
216 * Rebuild the parent for a directory that is not connected
217 *  to the filesystem root
218 */
219static
220struct inode *fat_rebuild_parent(struct super_block *sb, int parent_logstart)
221{
222	int search_clus, clus_to_match;
223	struct msdos_dir_entry *de;
224	struct inode *parent = NULL;
225	struct inode *dummy_grand_parent = NULL;
226	struct fat_slot_info sinfo;
227	struct msdos_sb_info *sbi = MSDOS_SB(sb);
228	sector_t blknr = fat_clus_to_blknr(sbi, parent_logstart);
229	struct buffer_head *parent_bh = sb_bread(sb, blknr);
230	if (!parent_bh) {
231		fat_msg(sb, KERN_ERR,
232			"unable to read cluster of parent directory");
233		return NULL;
234	}
235
236	de = (struct msdos_dir_entry *) parent_bh->b_data;
237	clus_to_match = fat_get_start(sbi, &de[0]);
238	search_clus = fat_get_start(sbi, &de[1]);
239
240	dummy_grand_parent = fat_dget(sb, search_clus);
241	if (!dummy_grand_parent) {
242		dummy_grand_parent = new_inode(sb);
243		if (!dummy_grand_parent) {
244			brelse(parent_bh);
245			return parent;
246		}
247
248		dummy_grand_parent->i_ino = iunique(sb, MSDOS_ROOT_INO);
249		fat_fill_inode(dummy_grand_parent, &de[1]);
250		MSDOS_I(dummy_grand_parent)->i_pos = -1;
251	}
252
253	if (!fat_scan_logstart(dummy_grand_parent, clus_to_match, &sinfo))
254		parent = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
255
256	brelse(parent_bh);
257	iput(dummy_grand_parent);
258
259	return parent;
260}
261
262/*
263 * Find the parent for a directory that is not currently connected to
264 * the filesystem root.
265 *
266 * On entry, the caller holds d_inode(child_dir)->i_mutex.
267 */
268static struct dentry *fat_get_parent(struct dentry *child_dir)
269{
270	struct super_block *sb = child_dir->d_sb;
271	struct buffer_head *bh = NULL;
272	struct msdos_dir_entry *de;
273	struct inode *parent_inode = NULL;
274	struct msdos_sb_info *sbi = MSDOS_SB(sb);
275
276	if (!fat_get_dotdot_entry(d_inode(child_dir), &bh, &de)) {
277		int parent_logstart = fat_get_start(sbi, de);
278		parent_inode = fat_dget(sb, parent_logstart);
279		if (!parent_inode && sbi->options.nfs == FAT_NFS_NOSTALE_RO)
280			parent_inode = fat_rebuild_parent(sb, parent_logstart);
281	}
282	brelse(bh);
283
284	return d_obtain_alias(parent_inode);
285}
286
287const struct export_operations fat_export_ops = {
288	.encode_fh	= generic_encode_ino32_fh,
289	.fh_to_dentry   = fat_fh_to_dentry,
290	.fh_to_parent   = fat_fh_to_parent,
291	.get_parent     = fat_get_parent,
292};
293
294const struct export_operations fat_export_ops_nostale = {
295	.encode_fh      = fat_encode_fh_nostale,
296	.fh_to_dentry   = fat_fh_to_dentry_nostale,
297	.fh_to_parent   = fat_fh_to_parent_nostale,
298	.get_parent     = fat_get_parent,
299};