1 /*
2  * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
3  *
4  * This software may be freely redistributed under the terms of the
5  * GNU General Public License.
6  *
7  * You should have received a copy of the GNU General Public License
8  * along with this program; if not, write to the Free Software
9  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
10  *
11  * Authors: David Woodhouse <dwmw2@infradead.org>
12  *          David Howells <dhowells@redhat.com>
13  *
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/fs.h>
20 #include <linux/pagemap.h>
21 #include <linux/sched.h>
22 #include <linux/mount.h>
23 #include <linux/namei.h>
24 #include <linux/iversion.h>
25 #include "internal.h"
26 
27 static const struct inode_operations afs_symlink_inode_operations = {
28 	.get_link	= page_get_link,
29 	.listxattr	= afs_listxattr,
30 };
31 
32 /*
33  * Initialise an inode from the vnode status.
34  */
afs_inode_init_from_status(struct afs_vnode * vnode,struct key * key)35 static int afs_inode_init_from_status(struct afs_vnode *vnode, struct key *key)
36 {
37 	struct inode *inode = AFS_VNODE_TO_I(vnode);
38 
39 	_debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
40 	       vnode->status.type,
41 	       vnode->status.nlink,
42 	       (unsigned long long) vnode->status.size,
43 	       vnode->status.data_version,
44 	       vnode->status.mode);
45 
46 	read_seqlock_excl(&vnode->cb_lock);
47 
48 	afs_update_inode_from_status(vnode, &vnode->status, NULL,
49 				     AFS_VNODE_NOT_YET_SET);
50 
51 	switch (vnode->status.type) {
52 	case AFS_FTYPE_FILE:
53 		inode->i_mode	= S_IFREG | vnode->status.mode;
54 		inode->i_op	= &afs_file_inode_operations;
55 		inode->i_fop	= &afs_file_operations;
56 		inode->i_mapping->a_ops	= &afs_fs_aops;
57 		break;
58 	case AFS_FTYPE_DIR:
59 		inode->i_mode	= S_IFDIR | vnode->status.mode;
60 		inode->i_op	= &afs_dir_inode_operations;
61 		inode->i_fop	= &afs_dir_file_operations;
62 		inode->i_mapping->a_ops	= &afs_dir_aops;
63 		break;
64 	case AFS_FTYPE_SYMLINK:
65 		/* Symlinks with a mode of 0644 are actually mountpoints. */
66 		if ((vnode->status.mode & 0777) == 0644) {
67 			inode->i_flags |= S_AUTOMOUNT;
68 
69 			set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
70 
71 			inode->i_mode	= S_IFDIR | 0555;
72 			inode->i_op	= &afs_mntpt_inode_operations;
73 			inode->i_fop	= &afs_mntpt_file_operations;
74 			inode->i_mapping->a_ops	= &afs_fs_aops;
75 		} else {
76 			inode->i_mode	= S_IFLNK | vnode->status.mode;
77 			inode->i_op	= &afs_symlink_inode_operations;
78 			inode->i_mapping->a_ops	= &afs_fs_aops;
79 		}
80 		inode_nohighmem(inode);
81 		break;
82 	default:
83 		printk("kAFS: AFS vnode with undefined type\n");
84 		read_sequnlock_excl(&vnode->cb_lock);
85 		return afs_protocol_error(NULL, -EBADMSG);
86 	}
87 
88 	inode->i_blocks		= 0;
89 	vnode->invalid_before	= vnode->status.data_version;
90 
91 	read_sequnlock_excl(&vnode->cb_lock);
92 	return 0;
93 }
94 
95 /*
96  * Fetch file status from the volume.
97  */
afs_fetch_status(struct afs_vnode * vnode,struct key * key,bool new_inode)98 int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool new_inode)
99 {
100 	struct afs_fs_cursor fc;
101 	int ret;
102 
103 	_enter("%s,{%x:%u.%u,S=%lx}",
104 	       vnode->volume->name,
105 	       vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
106 	       vnode->flags);
107 
108 	ret = -ERESTARTSYS;
109 	if (afs_begin_vnode_operation(&fc, vnode, key)) {
110 		while (afs_select_fileserver(&fc)) {
111 			fc.cb_break = afs_calc_vnode_cb_break(vnode);
112 			afs_fs_fetch_file_status(&fc, NULL, new_inode);
113 		}
114 
115 		afs_check_for_remote_deletion(&fc, fc.vnode);
116 		afs_vnode_commit_status(&fc, vnode, fc.cb_break);
117 		ret = afs_end_vnode_operation(&fc);
118 	}
119 
120 	_leave(" = %d", ret);
121 	return ret;
122 }
123 
124 /*
125  * iget5() comparator
126  */
afs_iget5_test(struct inode * inode,void * opaque)127 int afs_iget5_test(struct inode *inode, void *opaque)
128 {
129 	struct afs_iget_data *data = opaque;
130 
131 	return inode->i_ino == data->fid.vnode &&
132 		inode->i_generation == data->fid.unique;
133 }
134 
135 /*
136  * iget5() comparator for inode created by autocell operations
137  *
138  * These pseudo inodes don't match anything.
139  */
afs_iget5_pseudo_dir_test(struct inode * inode,void * opaque)140 static int afs_iget5_pseudo_dir_test(struct inode *inode, void *opaque)
141 {
142 	return 0;
143 }
144 
145 /*
146  * iget5() inode initialiser
147  */
afs_iget5_set(struct inode * inode,void * opaque)148 static int afs_iget5_set(struct inode *inode, void *opaque)
149 {
150 	struct afs_iget_data *data = opaque;
151 	struct afs_vnode *vnode = AFS_FS_I(inode);
152 
153 	inode->i_ino = data->fid.vnode;
154 	inode->i_generation = data->fid.unique;
155 	vnode->fid = data->fid;
156 	vnode->volume = data->volume;
157 
158 	return 0;
159 }
160 
161 /*
162  * Create an inode for a dynamic root directory or an autocell dynamic
163  * automount dir.
164  */
afs_iget_pseudo_dir(struct super_block * sb,bool root)165 struct inode *afs_iget_pseudo_dir(struct super_block *sb, bool root)
166 {
167 	struct afs_iget_data data;
168 	struct afs_super_info *as;
169 	struct afs_vnode *vnode;
170 	struct inode *inode;
171 	static atomic_t afs_autocell_ino;
172 
173 	_enter("");
174 
175 	as = sb->s_fs_info;
176 	if (as->volume) {
177 		data.volume = as->volume;
178 		data.fid.vid = as->volume->vid;
179 	}
180 	if (root) {
181 		data.fid.vnode = 1;
182 		data.fid.unique = 1;
183 	} else {
184 		data.fid.vnode = atomic_inc_return(&afs_autocell_ino);
185 		data.fid.unique = 0;
186 	}
187 
188 	inode = iget5_locked(sb, data.fid.vnode,
189 			     afs_iget5_pseudo_dir_test, afs_iget5_set,
190 			     &data);
191 	if (!inode) {
192 		_leave(" = -ENOMEM");
193 		return ERR_PTR(-ENOMEM);
194 	}
195 
196 	_debug("GOT INODE %p { ino=%lu, vl=%x, vn=%x, u=%x }",
197 	       inode, inode->i_ino, data.fid.vid, data.fid.vnode,
198 	       data.fid.unique);
199 
200 	vnode = AFS_FS_I(inode);
201 
202 	/* there shouldn't be an existing inode */
203 	BUG_ON(!(inode->i_state & I_NEW));
204 
205 	inode->i_size		= 0;
206 	inode->i_mode		= S_IFDIR | S_IRUGO | S_IXUGO;
207 	if (root) {
208 		inode->i_op	= &afs_dynroot_inode_operations;
209 		inode->i_fop	= &afs_dynroot_file_operations;
210 	} else {
211 		inode->i_op	= &afs_autocell_inode_operations;
212 	}
213 	set_nlink(inode, 2);
214 	inode->i_uid		= GLOBAL_ROOT_UID;
215 	inode->i_gid		= GLOBAL_ROOT_GID;
216 	inode->i_ctime.tv_sec	= get_seconds();
217 	inode->i_ctime.tv_nsec	= 0;
218 	inode->i_atime		= inode->i_mtime = inode->i_ctime;
219 	inode->i_blocks		= 0;
220 	inode_set_iversion_raw(inode, 0);
221 	inode->i_generation	= 0;
222 
223 	set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
224 	if (!root) {
225 		set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
226 		inode->i_flags |= S_AUTOMOUNT;
227 	}
228 
229 	inode->i_flags |= S_NOATIME;
230 	unlock_new_inode(inode);
231 	_leave(" = %p", inode);
232 	return inode;
233 }
234 
235 /*
236  * Get a cache cookie for an inode.
237  */
afs_get_inode_cache(struct afs_vnode * vnode)238 static void afs_get_inode_cache(struct afs_vnode *vnode)
239 {
240 #ifdef CONFIG_AFS_FSCACHE
241 	struct {
242 		u32 vnode_id;
243 		u32 unique;
244 		u32 vnode_id_ext[2];	/* Allow for a 96-bit key */
245 	} __packed key;
246 	struct afs_vnode_cache_aux aux;
247 
248 	if (vnode->status.type == AFS_FTYPE_DIR) {
249 		vnode->cache = NULL;
250 		return;
251 	}
252 
253 	key.vnode_id		= vnode->fid.vnode;
254 	key.unique		= vnode->fid.unique;
255 	key.vnode_id_ext[0]	= 0;
256 	key.vnode_id_ext[1]	= 0;
257 	aux.data_version	= vnode->status.data_version;
258 
259 	vnode->cache = fscache_acquire_cookie(vnode->volume->cache,
260 					      &afs_vnode_cache_index_def,
261 					      &key, sizeof(key),
262 					      &aux, sizeof(aux),
263 					      vnode, vnode->status.size, true);
264 #endif
265 }
266 
267 /*
268  * inode retrieval
269  */
afs_iget(struct super_block * sb,struct key * key,struct afs_fid * fid,struct afs_file_status * status,struct afs_callback * cb,struct afs_cb_interest * cbi)270 struct inode *afs_iget(struct super_block *sb, struct key *key,
271 		       struct afs_fid *fid, struct afs_file_status *status,
272 		       struct afs_callback *cb, struct afs_cb_interest *cbi)
273 {
274 	struct afs_iget_data data = { .fid = *fid };
275 	struct afs_super_info *as;
276 	struct afs_vnode *vnode;
277 	struct inode *inode;
278 	int ret;
279 
280 	_enter(",{%x:%u.%u},,", fid->vid, fid->vnode, fid->unique);
281 
282 	as = sb->s_fs_info;
283 	data.volume = as->volume;
284 
285 	inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
286 			     &data);
287 	if (!inode) {
288 		_leave(" = -ENOMEM");
289 		return ERR_PTR(-ENOMEM);
290 	}
291 
292 	_debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
293 	       inode, fid->vid, fid->vnode, fid->unique);
294 
295 	vnode = AFS_FS_I(inode);
296 
297 	/* deal with an existing inode */
298 	if (!(inode->i_state & I_NEW)) {
299 		_leave(" = %p", inode);
300 		return inode;
301 	}
302 
303 	if (!status) {
304 		/* it's a remotely extant inode */
305 		ret = afs_fetch_status(vnode, key, true);
306 		if (ret < 0)
307 			goto bad_inode;
308 	} else {
309 		/* it's an inode we just created */
310 		memcpy(&vnode->status, status, sizeof(vnode->status));
311 
312 		if (!cb) {
313 			/* it's a symlink we just created (the fileserver
314 			 * didn't give us a callback) */
315 			vnode->cb_version = 0;
316 			vnode->cb_type = 0;
317 			vnode->cb_expires_at = 0;
318 		} else {
319 			vnode->cb_version = cb->version;
320 			vnode->cb_type = cb->type;
321 			vnode->cb_expires_at = cb->expiry;
322 			vnode->cb_interest = afs_get_cb_interest(cbi);
323 			set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
324 		}
325 
326 		vnode->cb_expires_at += ktime_get_real_seconds();
327 	}
328 
329 	ret = afs_inode_init_from_status(vnode, key);
330 	if (ret < 0)
331 		goto bad_inode;
332 
333 	afs_get_inode_cache(vnode);
334 
335 	/* success */
336 	clear_bit(AFS_VNODE_UNSET, &vnode->flags);
337 	inode->i_flags |= S_NOATIME;
338 	unlock_new_inode(inode);
339 	_leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
340 	return inode;
341 
342 	/* failure */
343 bad_inode:
344 	iget_failed(inode);
345 	_leave(" = %d [bad]", ret);
346 	return ERR_PTR(ret);
347 }
348 
349 /*
350  * mark the data attached to an inode as obsolete due to a write on the server
351  * - might also want to ditch all the outstanding writes and dirty pages
352  */
afs_zap_data(struct afs_vnode * vnode)353 void afs_zap_data(struct afs_vnode *vnode)
354 {
355 	_enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
356 
357 #ifdef CONFIG_AFS_FSCACHE
358 	fscache_invalidate(vnode->cache);
359 #endif
360 
361 	/* nuke all the non-dirty pages that aren't locked, mapped or being
362 	 * written back in a regular file and completely discard the pages in a
363 	 * directory or symlink */
364 	if (S_ISREG(vnode->vfs_inode.i_mode))
365 		invalidate_remote_inode(&vnode->vfs_inode);
366 	else
367 		invalidate_inode_pages2(vnode->vfs_inode.i_mapping);
368 }
369 
370 /*
371  * validate a vnode/inode
372  * - there are several things we need to check
373  *   - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
374  *     symlink)
375  *   - parent dir metadata changed (security changes)
376  *   - dentry data changed (write, truncate)
377  *   - dentry metadata changed (security changes)
378  */
afs_validate(struct afs_vnode * vnode,struct key * key)379 int afs_validate(struct afs_vnode *vnode, struct key *key)
380 {
381 	time64_t now = ktime_get_real_seconds();
382 	bool valid;
383 	int ret;
384 
385 	_enter("{v={%x:%u} fl=%lx},%x",
386 	       vnode->fid.vid, vnode->fid.vnode, vnode->flags,
387 	       key_serial(key));
388 
389 	/* Quickly check the callback state.  Ideally, we'd use read_seqbegin
390 	 * here, but we have no way to pass the net namespace to the RCU
391 	 * cleanup for the server record.
392 	 */
393 	read_seqlock_excl(&vnode->cb_lock);
394 
395 	if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
396 		if (vnode->cb_s_break != vnode->cb_interest->server->cb_s_break ||
397 		    vnode->cb_v_break != vnode->volume->cb_v_break) {
398 			vnode->cb_s_break = vnode->cb_interest->server->cb_s_break;
399 			vnode->cb_v_break = vnode->volume->cb_v_break;
400 			valid = false;
401 		} else if (test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
402 			valid = false;
403 		} else if (vnode->cb_expires_at - 10 <= now) {
404 			valid = false;
405 		} else {
406 			valid = true;
407 		}
408 	} else if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
409 		valid = true;
410 	} else {
411 		vnode->cb_v_break = vnode->volume->cb_v_break;
412 		valid = false;
413 	}
414 
415 	read_sequnlock_excl(&vnode->cb_lock);
416 
417 	if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
418 		clear_nlink(&vnode->vfs_inode);
419 
420 	if (valid)
421 		goto valid;
422 
423 	down_write(&vnode->validate_lock);
424 
425 	/* if the promise has expired, we need to check the server again to get
426 	 * a new promise - note that if the (parent) directory's metadata was
427 	 * changed then the security may be different and we may no longer have
428 	 * access */
429 	if (!test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
430 		_debug("not promised");
431 		ret = afs_fetch_status(vnode, key, false);
432 		if (ret < 0) {
433 			if (ret == -ENOENT) {
434 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
435 				ret = -ESTALE;
436 			}
437 			goto error_unlock;
438 		}
439 		_debug("new promise [fl=%lx]", vnode->flags);
440 	}
441 
442 	if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
443 		_debug("file already deleted");
444 		ret = -ESTALE;
445 		goto error_unlock;
446 	}
447 
448 	/* if the vnode's data version number changed then its contents are
449 	 * different */
450 	if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
451 		afs_zap_data(vnode);
452 	up_write(&vnode->validate_lock);
453 valid:
454 	_leave(" = 0");
455 	return 0;
456 
457 error_unlock:
458 	up_write(&vnode->validate_lock);
459 	_leave(" = %d", ret);
460 	return ret;
461 }
462 
463 /*
464  * read the attributes of an inode
465  */
afs_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)466 int afs_getattr(const struct path *path, struct kstat *stat,
467 		u32 request_mask, unsigned int query_flags)
468 {
469 	struct inode *inode = d_inode(path->dentry);
470 	struct afs_vnode *vnode = AFS_FS_I(inode);
471 	struct key *key;
472 	int ret, seq = 0;
473 
474 	_enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation);
475 
476 	if (vnode->volume &&
477 	    !(query_flags & AT_STATX_DONT_SYNC) &&
478 	    !test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
479 		key = afs_request_key(vnode->volume->cell);
480 		if (IS_ERR(key))
481 			return PTR_ERR(key);
482 		ret = afs_validate(vnode, key);
483 		key_put(key);
484 		if (ret < 0)
485 			return ret;
486 	}
487 
488 	do {
489 		read_seqbegin_or_lock(&vnode->cb_lock, &seq);
490 		generic_fillattr(inode, stat);
491 	} while (need_seqretry(&vnode->cb_lock, seq));
492 
493 	done_seqretry(&vnode->cb_lock, seq);
494 	return 0;
495 }
496 
497 /*
498  * discard an AFS inode
499  */
afs_drop_inode(struct inode * inode)500 int afs_drop_inode(struct inode *inode)
501 {
502 	_enter("");
503 
504 	if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags))
505 		return generic_delete_inode(inode);
506 	else
507 		return generic_drop_inode(inode);
508 }
509 
510 /*
511  * clear an AFS inode
512  */
afs_evict_inode(struct inode * inode)513 void afs_evict_inode(struct inode *inode)
514 {
515 	struct afs_vnode *vnode;
516 
517 	vnode = AFS_FS_I(inode);
518 
519 	_enter("{%x:%u.%d}",
520 	       vnode->fid.vid,
521 	       vnode->fid.vnode,
522 	       vnode->fid.unique);
523 
524 	_debug("CLEAR INODE %p", inode);
525 
526 	ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
527 
528 	truncate_inode_pages_final(&inode->i_data);
529 	clear_inode(inode);
530 
531 	if (vnode->cb_interest) {
532 		afs_put_cb_interest(afs_i2net(inode), vnode->cb_interest);
533 		vnode->cb_interest = NULL;
534 	}
535 
536 	while (!list_empty(&vnode->wb_keys)) {
537 		struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next,
538 						    struct afs_wb_key, vnode_link);
539 		list_del(&wbk->vnode_link);
540 		afs_put_wb_key(wbk);
541 	}
542 
543 #ifdef CONFIG_AFS_FSCACHE
544 	{
545 		struct afs_vnode_cache_aux aux;
546 
547 		aux.data_version = vnode->status.data_version;
548 		fscache_relinquish_cookie(vnode->cache, &aux,
549 					  test_bit(AFS_VNODE_DELETED, &vnode->flags));
550 		vnode->cache = NULL;
551 	}
552 #endif
553 
554 	afs_prune_wb_keys(vnode);
555 	afs_put_permits(rcu_access_pointer(vnode->permit_cache));
556 	key_put(vnode->lock_key);
557 	vnode->lock_key = NULL;
558 	_leave("");
559 }
560 
561 /*
562  * set the attributes of an inode
563  */
afs_setattr(struct dentry * dentry,struct iattr * attr)564 int afs_setattr(struct dentry *dentry, struct iattr *attr)
565 {
566 	struct afs_fs_cursor fc;
567 	struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
568 	struct key *key;
569 	int ret;
570 
571 	_enter("{%x:%u},{n=%pd},%x",
572 	       vnode->fid.vid, vnode->fid.vnode, dentry,
573 	       attr->ia_valid);
574 
575 	if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
576 				ATTR_MTIME))) {
577 		_leave(" = 0 [unsupported]");
578 		return 0;
579 	}
580 
581 	/* flush any dirty data outstanding on a regular file */
582 	if (S_ISREG(vnode->vfs_inode.i_mode))
583 		filemap_write_and_wait(vnode->vfs_inode.i_mapping);
584 
585 	if (attr->ia_valid & ATTR_FILE) {
586 		key = afs_file_key(attr->ia_file);
587 	} else {
588 		key = afs_request_key(vnode->volume->cell);
589 		if (IS_ERR(key)) {
590 			ret = PTR_ERR(key);
591 			goto error;
592 		}
593 	}
594 
595 	ret = -ERESTARTSYS;
596 	if (afs_begin_vnode_operation(&fc, vnode, key)) {
597 		while (afs_select_fileserver(&fc)) {
598 			fc.cb_break = afs_calc_vnode_cb_break(vnode);
599 			afs_fs_setattr(&fc, attr);
600 		}
601 
602 		afs_check_for_remote_deletion(&fc, fc.vnode);
603 		afs_vnode_commit_status(&fc, vnode, fc.cb_break);
604 		ret = afs_end_vnode_operation(&fc);
605 	}
606 
607 	if (!(attr->ia_valid & ATTR_FILE))
608 		key_put(key);
609 
610 error:
611 	_leave(" = %d", ret);
612 	return ret;
613 }
614