xref: /linux/fs/afs/inode.c (revision 5dfa01ef37a8b944773aef8dee747cd76dec4234)
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 #include "afs_fs.h"
27 
dump_vnode(struct afs_vnode * vnode,struct afs_vnode * parent_vnode)28 static noinline void dump_vnode(struct afs_vnode *vnode, struct afs_vnode *parent_vnode)
29 {
30 	static unsigned long once_only;
31 
32 	pr_warn("kAFS: AFS vnode with undefined type %u\n", vnode->status.type);
33 	pr_warn("kAFS: A=%d m=%o s=%llx v=%llx\n",
34 		vnode->status.abort_code,
35 		vnode->status.mode,
36 		vnode->status.size,
37 		vnode->status.data_version);
38 	pr_warn("kAFS: vnode %llx:%llx:%x\n",
39 		vnode->fid.vid,
40 		vnode->fid.vnode,
41 		vnode->fid.unique);
42 	if (parent_vnode)
43 		pr_warn("kAFS: dir %llx:%llx:%x\n",
44 			parent_vnode->fid.vid,
45 			parent_vnode->fid.vnode,
46 			parent_vnode->fid.unique);
47 
48 	if (!test_and_set_bit(0, &once_only))
49 		dump_stack();
50 }
51 
52 /*
53  * Set parameters for the netfs library
54  */
afs_set_netfs_context(struct afs_vnode * vnode)55 static void afs_set_netfs_context(struct afs_vnode *vnode)
56 {
57 	netfs_inode_init(&vnode->netfs, &afs_req_ops, true);
58 }
59 
60 /*
61  * Initialise an inode from the vnode status.
62  */
afs_inode_init_from_status(struct afs_operation * op,struct afs_vnode_param * vp,struct afs_vnode * vnode)63 static int afs_inode_init_from_status(struct afs_operation *op,
64 				      struct afs_vnode_param *vp,
65 				      struct afs_vnode *vnode)
66 {
67 	struct afs_file_status *status = &vp->scb.status;
68 	struct inode *inode = AFS_VNODE_TO_I(vnode);
69 	struct timespec64 t;
70 
71 	_enter("{%llx:%llu.%u} %s",
72 	       vp->fid.vid, vp->fid.vnode, vp->fid.unique,
73 	       op->type ? op->type->name : "???");
74 
75 	_debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
76 	       status->type,
77 	       status->nlink,
78 	       (unsigned long long) status->size,
79 	       status->data_version,
80 	       status->mode);
81 
82 	write_seqlock(&vnode->cb_lock);
83 
84 	vnode->cb_v_check = op->cb_v_break;
85 	vnode->status = *status;
86 
87 	t = status->mtime_client;
88 	inode_set_ctime_to_ts(inode, t);
89 	inode_set_mtime_to_ts(inode, t);
90 	inode_set_atime_to_ts(inode, t);
91 	inode->i_flags |= S_NOATIME;
92 	inode->i_uid = make_kuid(&init_user_ns, status->owner);
93 	inode->i_gid = make_kgid(&init_user_ns, status->group);
94 	set_nlink(&vnode->netfs.inode, status->nlink);
95 
96 	switch (status->type) {
97 	case AFS_FTYPE_FILE:
98 		inode->i_mode	= S_IFREG | (status->mode & S_IALLUGO);
99 		inode->i_op	= &afs_file_inode_operations;
100 		inode->i_fop	= &afs_file_operations;
101 		inode->i_mapping->a_ops	= &afs_file_aops;
102 		mapping_set_large_folios(inode->i_mapping);
103 		break;
104 	case AFS_FTYPE_DIR:
105 		inode->i_mode	= S_IFDIR |  (status->mode & S_IALLUGO);
106 		inode->i_op	= &afs_dir_inode_operations;
107 		inode->i_fop	= &afs_dir_file_operations;
108 		inode->i_mapping->a_ops	= &afs_dir_aops;
109 		__set_bit(NETFS_ICTX_SINGLE_NO_UPLOAD, &vnode->netfs.flags);
110 		/* Assume locally cached directory data will be valid. */
111 		__set_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
112 		break;
113 	case AFS_FTYPE_SYMLINK:
114 		/* Symlinks with a mode of 0644 are actually mountpoints. */
115 		if ((status->mode & 0777) == 0644) {
116 			inode->i_flags |= S_AUTOMOUNT;
117 
118 			set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
119 
120 			inode->i_mode	= S_IFDIR | 0555;
121 			inode->i_op	= &afs_mntpt_inode_operations;
122 			inode->i_fop	= &afs_mntpt_file_operations;
123 		} else {
124 			inode->i_mode	= S_IFLNK | status->mode;
125 			inode->i_op	= &afs_symlink_inode_operations;
126 		}
127 		inode->i_mapping->a_ops	= &afs_symlink_aops;
128 		inode_nohighmem(inode);
129 		mapping_set_release_always(inode->i_mapping);
130 		break;
131 	default:
132 		dump_vnode(vnode, op->file[0].vnode != vnode ? op->file[0].vnode : NULL);
133 		write_sequnlock(&vnode->cb_lock);
134 		return afs_protocol_error(NULL, afs_eproto_file_type);
135 	}
136 
137 	i_size_write(inode, status->size);
138 	inode_set_bytes(inode, status->size);
139 	afs_set_netfs_context(vnode);
140 
141 	vnode->invalid_before	= status->data_version;
142 	trace_afs_set_dv(vnode, status->data_version);
143 	inode_set_iversion_raw(&vnode->netfs.inode, status->data_version);
144 
145 	if (!vp->scb.have_cb) {
146 		/* it's a symlink we just created (the fileserver
147 		 * didn't give us a callback) */
148 		afs_clear_cb_promise(vnode, afs_cb_promise_set_new_symlink);
149 	} else {
150 		vnode->cb_server = op->server;
151 		afs_set_cb_promise(vnode, vp->scb.callback.expires_at,
152 				   afs_cb_promise_set_new_inode);
153 	}
154 
155 	write_sequnlock(&vnode->cb_lock);
156 	return 0;
157 }
158 
159 /*
160  * Update the core inode struct from a returned status record.
161  */
afs_apply_status(struct afs_operation * op,struct afs_vnode_param * vp)162 static void afs_apply_status(struct afs_operation *op,
163 			     struct afs_vnode_param *vp)
164 {
165 	struct afs_file_status *status = &vp->scb.status;
166 	struct afs_vnode *vnode = vp->vnode;
167 	struct netfs_inode *ictx = &vnode->netfs;
168 	struct inode *inode = &ictx->inode;
169 	struct timespec64 t;
170 	umode_t mode;
171 	bool unexpected_jump = false;
172 	bool data_changed = false;
173 	bool change_size = vp->set_size;
174 
175 	_enter("{%llx:%llu.%u} %s",
176 	       vp->fid.vid, vp->fid.vnode, vp->fid.unique,
177 	       op->type ? op->type->name : "???");
178 
179 	BUG_ON(test_bit(AFS_VNODE_UNSET, &vnode->flags));
180 
181 	if (status->type != vnode->status.type) {
182 		pr_warn("Vnode %llx:%llx:%x changed type %u to %u\n",
183 			vnode->fid.vid,
184 			vnode->fid.vnode,
185 			vnode->fid.unique,
186 			status->type, vnode->status.type);
187 		afs_protocol_error(NULL, afs_eproto_bad_status);
188 		return;
189 	}
190 
191 	if (status->nlink != vnode->status.nlink)
192 		set_nlink(inode, status->nlink);
193 
194 	if (status->owner != vnode->status.owner)
195 		inode->i_uid = make_kuid(&init_user_ns, status->owner);
196 
197 	if (status->group != vnode->status.group)
198 		inode->i_gid = make_kgid(&init_user_ns, status->group);
199 
200 	if (status->mode != vnode->status.mode) {
201 		mode = inode->i_mode;
202 		mode &= ~S_IALLUGO;
203 		mode |= status->mode & S_IALLUGO;
204 		WRITE_ONCE(inode->i_mode, mode);
205 	}
206 
207 	t = status->mtime_client;
208 	inode_set_mtime_to_ts(inode, t);
209 	if (vp->update_ctime)
210 		inode_set_ctime_to_ts(inode, op->ctime);
211 
212 	if (vnode->status.data_version != status->data_version) {
213 		trace_afs_set_dv(vnode, status->data_version);
214 		data_changed = true;
215 	}
216 
217 	vnode->status = *status;
218 
219 	if (vp->dv_before + vp->dv_delta != status->data_version) {
220 		trace_afs_dv_mismatch(vnode, vp->dv_before, vp->dv_delta,
221 				      status->data_version);
222 
223 		if (vnode->cb_ro_snapshot == atomic_read(&vnode->volume->cb_ro_snapshot) &&
224 		    atomic64_read(&vnode->cb_expires_at) != AFS_NO_CB_PROMISE)
225 			pr_warn("kAFS: vnode modified {%llx:%llu} %llx->%llx %s (op=%x)\n",
226 				vnode->fid.vid, vnode->fid.vnode,
227 				(unsigned long long)vp->dv_before + vp->dv_delta,
228 				(unsigned long long)status->data_version,
229 				op->type ? op->type->name : "???",
230 				op->debug_id);
231 
232 		vnode->invalid_before = status->data_version;
233 		if (vnode->status.type == AFS_FTYPE_DIR)
234 			afs_invalidate_dir(vnode, afs_dir_invalid_dv_mismatch);
235 		else
236 			set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
237 		change_size = true;
238 		data_changed = true;
239 		unexpected_jump = true;
240 	} else if (vnode->status.type == AFS_FTYPE_DIR) {
241 		/* Expected directory change is handled elsewhere so
242 		 * that we can locally edit the directory and save on a
243 		 * download.
244 		 */
245 		if (test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
246 			data_changed = false;
247 		change_size = true;
248 	}
249 
250 	if (data_changed) {
251 		unsigned long long zero_point, size = status->size;
252 
253 		inode_set_iversion_raw(inode, status->data_version);
254 
255 		/* Only update the size if the data version jumped.  If the
256 		 * file is being modified locally, then we might have our own
257 		 * idea of what the size should be that's not the same as
258 		 * what's on the server.
259 		 */
260 		spin_lock(&inode->i_lock);
261 
262 		if (change_size || size > i_size_read(inode)) {
263 			/* We can read the sizes directly as we hold i_lock. */
264 			zero_point = ictx->_zero_point;
265 
266 			if (unexpected_jump)
267 				zero_point = size;
268 			netfs_write_sizes(inode, size, size, zero_point);
269 			inode_set_bytes(inode, size);
270 			inode_set_ctime_to_ts(inode, t);
271 			inode_set_atime_to_ts(inode, t);
272 		} else {
273 			netfs_write_remote_i_size(inode, size);
274 		}
275 		spin_unlock(&inode->i_lock);
276 
277 		if (op->ops == &afs_fetch_data_operation)
278 			op->fetch.subreq->rreq->i_size = size;
279 	}
280 }
281 
282 /*
283  * Apply a callback to a vnode.
284  */
afs_apply_callback(struct afs_operation * op,struct afs_vnode_param * vp)285 static void afs_apply_callback(struct afs_operation *op,
286 			       struct afs_vnode_param *vp)
287 {
288 	struct afs_callback *cb = &vp->scb.callback;
289 	struct afs_vnode *vnode = vp->vnode;
290 
291 	if (!afs_cb_is_broken(vp->cb_break_before, vnode)) {
292 		if (op->volume->type == AFSVL_RWVOL)
293 			vnode->cb_server = op->server;
294 		afs_set_cb_promise(vnode, cb->expires_at, afs_cb_promise_set_apply_cb);
295 	}
296 }
297 
298 /*
299  * Apply the received status and callback to an inode all in the same critical
300  * section to avoid races with afs_validate().
301  */
afs_vnode_commit_status(struct afs_operation * op,struct afs_vnode_param * vp)302 void afs_vnode_commit_status(struct afs_operation *op, struct afs_vnode_param *vp)
303 {
304 	struct afs_vnode *vnode = vp->vnode;
305 
306 	_enter("");
307 
308 	write_seqlock(&vnode->cb_lock);
309 
310 	if (vp->scb.have_error) {
311 		/* A YFS server will return this from RemoveFile2 and AFS and
312 		 * YFS will return this from InlineBulkStatus.
313 		 */
314 		if (vp->scb.status.abort_code == VNOVNODE) {
315 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
316 			clear_nlink(&vnode->netfs.inode);
317 			__afs_break_callback(vnode, afs_cb_break_for_deleted);
318 			op->flags &= ~AFS_OPERATION_DIR_CONFLICT;
319 		}
320 	} else if (vp->scb.have_status) {
321 		if (vp->speculative &&
322 		    (test_bit(AFS_VNODE_MODIFYING, &vnode->flags) ||
323 		     vp->dv_before != vnode->status.data_version))
324 			/* Ignore the result of a speculative bulk status fetch
325 			 * if it splits around a modification op, thereby
326 			 * appearing to regress the data version.
327 			 */
328 			goto out;
329 		afs_apply_status(op, vp);
330 		if (vp->scb.have_cb)
331 			afs_apply_callback(op, vp);
332 	} else if (vp->op_unlinked && !(op->flags & AFS_OPERATION_DIR_CONFLICT)) {
333 		drop_nlink(&vnode->netfs.inode);
334 		if (vnode->netfs.inode.i_nlink == 0) {
335 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
336 			__afs_break_callback(vnode, afs_cb_break_for_deleted);
337 		}
338 	}
339 
340 out:
341 	write_sequnlock(&vnode->cb_lock);
342 
343 	if (vp->scb.have_status)
344 		afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb);
345 }
346 
afs_fetch_status_success(struct afs_operation * op)347 static void afs_fetch_status_success(struct afs_operation *op)
348 {
349 	struct afs_vnode_param *vp = &op->file[op->fetch_status.which];
350 	struct afs_vnode *vnode = vp->vnode;
351 	int ret;
352 
353 	if (inode_state_read_once(&vnode->netfs.inode) & I_NEW) {
354 		ret = afs_inode_init_from_status(op, vp, vnode);
355 		afs_op_set_error(op, ret);
356 		if (ret == 0)
357 			afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb);
358 	} else {
359 		afs_vnode_commit_status(op, vp);
360 	}
361 }
362 
363 const struct afs_operation_ops afs_fetch_status_operation = {
364 	.issue_afs_rpc	= afs_fs_fetch_status,
365 	.issue_yfs_rpc	= yfs_fs_fetch_status,
366 	.success	= afs_fetch_status_success,
367 	.aborted	= afs_check_for_remote_deletion,
368 };
369 
370 /*
371  * Fetch file status from the volume.
372  */
afs_fetch_status(struct afs_vnode * vnode,struct key * key,bool is_new,afs_access_t * _caller_access)373 int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool is_new,
374 		     afs_access_t *_caller_access)
375 {
376 	struct afs_operation *op;
377 
378 	_enter("%s,{%llx:%llu.%u,S=%lx}",
379 	       vnode->volume->name,
380 	       vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
381 	       vnode->flags);
382 
383 	op = afs_alloc_operation(key, vnode->volume);
384 	if (IS_ERR(op))
385 		return PTR_ERR(op);
386 
387 	afs_op_set_vnode(op, 0, vnode);
388 
389 	op->nr_files	= 1;
390 	op->ops		= &afs_fetch_status_operation;
391 	afs_begin_vnode_operation(op);
392 	afs_wait_for_operation(op);
393 
394 	if (_caller_access)
395 		*_caller_access = op->file[0].scb.status.caller_access;
396 	return afs_put_operation(op);
397 }
398 
399 /*
400  * ilookup() comparator
401  */
afs_ilookup5_test_by_fid(struct inode * inode,void * opaque)402 int afs_ilookup5_test_by_fid(struct inode *inode, void *opaque)
403 {
404 	struct afs_vnode *vnode = AFS_FS_I(inode);
405 	struct afs_fid *fid = opaque;
406 
407 	return (fid->vnode == vnode->fid.vnode &&
408 		fid->vnode_hi == vnode->fid.vnode_hi &&
409 		fid->unique == vnode->fid.unique);
410 }
411 
412 /*
413  * iget5() comparator
414  */
afs_iget5_test(struct inode * inode,void * opaque)415 static int afs_iget5_test(struct inode *inode, void *opaque)
416 {
417 	struct afs_vnode_param *vp = opaque;
418 	//struct afs_vnode *vnode = AFS_FS_I(inode);
419 
420 	return afs_ilookup5_test_by_fid(inode, &vp->fid);
421 }
422 
423 /*
424  * iget5() inode initialiser
425  */
afs_iget5_set(struct inode * inode,void * opaque)426 static int afs_iget5_set(struct inode *inode, void *opaque)
427 {
428 	struct afs_vnode_param *vp = opaque;
429 	struct afs_super_info *as = AFS_FS_S(inode->i_sb);
430 	struct afs_vnode *vnode = AFS_FS_I(inode);
431 
432 	vnode->volume		= as->volume;
433 	vnode->fid		= vp->fid;
434 
435 	/* YFS supports 96-bit vnode IDs, but Linux only supports
436 	 * 64-bit inode numbers.
437 	 */
438 	inode->i_ino		= vnode->fid.vnode;
439 	inode->i_generation	= vnode->fid.unique;
440 	return 0;
441 }
442 
443 /*
444  * Get a cache cookie for an inode.
445  */
afs_get_inode_cache(struct afs_vnode * vnode)446 static void afs_get_inode_cache(struct afs_vnode *vnode)
447 {
448 #ifdef CONFIG_AFS_FSCACHE
449 	struct {
450 		__be32 vnode_id;
451 		__be32 unique;
452 		__be32 vnode_id_ext[2];	/* Allow for a 96-bit key */
453 	} __packed key;
454 	struct afs_vnode_cache_aux aux;
455 
456 	if (vnode->status.type != AFS_FTYPE_FILE &&
457 	    vnode->status.type != AFS_FTYPE_DIR &&
458 	    vnode->status.type != AFS_FTYPE_SYMLINK) {
459 		vnode->netfs.cache = NULL;
460 		return;
461 	}
462 
463 	key.vnode_id		= htonl(vnode->fid.vnode);
464 	key.unique		= htonl(vnode->fid.unique);
465 	key.vnode_id_ext[0]	= htonl(vnode->fid.vnode >> 32);
466 	key.vnode_id_ext[1]	= htonl(vnode->fid.vnode_hi);
467 	afs_set_cache_aux(vnode, &aux);
468 
469 	afs_vnode_set_cache(vnode,
470 			    fscache_acquire_cookie(
471 				    vnode->volume->cache,
472 				    vnode->status.type == AFS_FTYPE_FILE ?
473 				    0 : FSCACHE_ADV_SINGLE_CHUNK,
474 				    &key, sizeof(key),
475 				    &aux, sizeof(aux),
476 				    i_size_read(&vnode->netfs.inode)));
477 #endif
478 }
479 
480 /*
481  * inode retrieval
482  */
afs_iget(struct afs_operation * op,struct afs_vnode_param * vp)483 struct inode *afs_iget(struct afs_operation *op, struct afs_vnode_param *vp)
484 {
485 	struct afs_vnode_param *dvp = &op->file[0];
486 	struct super_block *sb = dvp->vnode->netfs.inode.i_sb;
487 	struct afs_vnode *vnode;
488 	struct inode *inode;
489 	int ret;
490 
491 	_enter(",{%llx:%llu.%u},,", vp->fid.vid, vp->fid.vnode, vp->fid.unique);
492 
493 	inode = iget5_locked(sb, vp->fid.vnode, afs_iget5_test, afs_iget5_set, vp);
494 	if (!inode) {
495 		_leave(" = -ENOMEM");
496 		return ERR_PTR(-ENOMEM);
497 	}
498 
499 	vnode = AFS_FS_I(inode);
500 
501 	_debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }",
502 	       inode, vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
503 
504 	/* deal with an existing inode */
505 	if (!(inode_state_read_once(inode) & I_NEW)) {
506 		_leave(" = %p", inode);
507 		return inode;
508 	}
509 
510 	ret = afs_inode_init_from_status(op, vp, vnode);
511 	if (ret < 0)
512 		goto bad_inode;
513 
514 	afs_get_inode_cache(vnode);
515 
516 	/* success */
517 	clear_bit(AFS_VNODE_UNSET, &vnode->flags);
518 	unlock_new_inode(inode);
519 	_leave(" = %p", inode);
520 	return inode;
521 
522 	/* failure */
523 bad_inode:
524 	iget_failed(inode);
525 	_leave(" = %d [bad]", ret);
526 	return ERR_PTR(ret);
527 }
528 
afs_iget5_set_root(struct inode * inode,void * opaque)529 static int afs_iget5_set_root(struct inode *inode, void *opaque)
530 {
531 	struct afs_super_info *as = AFS_FS_S(inode->i_sb);
532 	struct afs_vnode *vnode = AFS_FS_I(inode);
533 
534 	vnode->volume		= as->volume;
535 	vnode->fid.vid		= as->volume->vid;
536 	vnode->fid.vnode	= 1;
537 	vnode->fid.unique	= 1;
538 	inode->i_ino		= 1;
539 	inode->i_generation	= 1;
540 	return 0;
541 }
542 
543 /*
544  * Set up the root inode for a volume.  This is always vnode 1, unique 1 within
545  * the volume.
546  */
afs_root_iget(struct super_block * sb,struct key * key)547 struct inode *afs_root_iget(struct super_block *sb, struct key *key)
548 {
549 	struct afs_super_info *as = AFS_FS_S(sb);
550 	struct afs_operation *op;
551 	struct afs_vnode *vnode;
552 	struct inode *inode;
553 	int ret;
554 
555 	_enter(",{%llx},,", as->volume->vid);
556 
557 	inode = iget5_locked(sb, 1, NULL, afs_iget5_set_root, NULL);
558 	if (!inode) {
559 		_leave(" = -ENOMEM");
560 		return ERR_PTR(-ENOMEM);
561 	}
562 
563 	_debug("GOT ROOT INODE %p { vl=%llx }", inode, as->volume->vid);
564 
565 	BUG_ON(!(inode_state_read_once(inode) & I_NEW));
566 
567 	vnode = AFS_FS_I(inode);
568 	vnode->cb_v_check = atomic_read(&as->volume->cb_v_break);
569 	afs_set_netfs_context(vnode);
570 
571 	op = afs_alloc_operation(key, as->volume);
572 	if (IS_ERR(op)) {
573 		ret = PTR_ERR(op);
574 		goto error;
575 	}
576 
577 	afs_op_set_vnode(op, 0, vnode);
578 
579 	op->nr_files	= 1;
580 	op->ops		= &afs_fetch_status_operation;
581 	ret = afs_do_sync_operation(op);
582 	if (ret < 0)
583 		goto error;
584 
585 	afs_get_inode_cache(vnode);
586 
587 	clear_bit(AFS_VNODE_UNSET, &vnode->flags);
588 	unlock_new_inode(inode);
589 	_leave(" = %p", inode);
590 	return inode;
591 
592 error:
593 	iget_failed(inode);
594 	_leave(" = %d [bad]", ret);
595 	return ERR_PTR(ret);
596 }
597 
598 /*
599  * read the attributes of an inode
600  */
afs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)601 int afs_getattr(struct mnt_idmap *idmap, const struct path *path,
602 		struct kstat *stat, u32 request_mask, unsigned int query_flags)
603 {
604 	struct inode *inode = d_inode(path->dentry);
605 	struct afs_vnode *vnode = AFS_FS_I(inode);
606 	struct key *key;
607 	int ret, seq;
608 
609 	_enter("{ ino=%llu v=%u }", inode->i_ino, inode->i_generation);
610 
611 	if (vnode->volume &&
612 	    !(query_flags & AT_STATX_DONT_SYNC) &&
613 	    atomic64_read(&vnode->cb_expires_at) == AFS_NO_CB_PROMISE) {
614 		key = afs_request_key(vnode->volume->cell);
615 		if (IS_ERR(key))
616 			return PTR_ERR(key);
617 		ret = afs_validate(vnode, key);
618 		key_put(key);
619 		if (ret < 0)
620 			return ret;
621 	}
622 
623 	do {
624 		seq = read_seqbegin(&vnode->cb_lock);
625 		generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
626 		if (test_bit(AFS_VNODE_SILLY_DELETED, &vnode->flags) &&
627 		    stat->nlink > 0)
628 			stat->nlink -= 1;
629 
630 		/* Lie about the size of directories.  We maintain a locally
631 		 * edited copy and may make different allocation decisions on
632 		 * it, but we need to give userspace the server's size.
633 		 */
634 		if (S_ISDIR(inode->i_mode))
635 			stat->size = netfs_read_remote_i_size(inode);
636 	} while (read_seqretry(&vnode->cb_lock, seq));
637 
638 	return 0;
639 }
640 
641 /*
642  * discard an AFS inode
643  */
afs_drop_inode(struct inode * inode)644 int afs_drop_inode(struct inode *inode)
645 {
646 	_enter("");
647 
648 	if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags))
649 		return inode_just_drop(inode);
650 	else
651 		return inode_generic_drop(inode);
652 }
653 
654 /*
655  * clear an AFS inode
656  */
afs_evict_inode(struct inode * inode)657 void afs_evict_inode(struct inode *inode)
658 {
659 	struct afs_vnode_cache_aux aux;
660 	struct afs_super_info *sbi = AFS_FS_S(inode->i_sb);
661 	struct afs_vnode *vnode = AFS_FS_I(inode);
662 
663 	_enter("{%llx:%llu.%d}",
664 	       vnode->fid.vid,
665 	       vnode->fid.vnode,
666 	       vnode->fid.unique);
667 
668 	_debug("CLEAR INODE %p", inode);
669 
670 	ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
671 
672 	if ((S_ISDIR(inode->i_mode) ||
673 	     S_ISLNK(inode->i_mode)) &&
674 	    (inode_state_read_once(inode) & I_DIRTY) &&
675 	    !sbi->dyn_root) {
676 		struct writeback_control wbc = {
677 			.sync_mode = WB_SYNC_ALL,
678 			.for_sync = true,
679 			.range_end = LLONG_MAX,
680 		};
681 
682 		inode->i_mapping->a_ops->writepages(inode->i_mapping, &wbc);
683 	}
684 
685 	netfs_wait_for_outstanding_io(inode);
686 	truncate_inode_pages_final(&inode->i_data);
687 	netfs_free_folioq_buffer(vnode->directory);
688 	if (vnode->symlink)
689 		afs_evict_symlink(vnode);
690 
691 	afs_set_cache_aux(vnode, &aux);
692 	netfs_clear_inode_writeback(inode, &aux);
693 	clear_inode(inode);
694 
695 	while (!list_empty(&vnode->wb_keys)) {
696 		struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next,
697 						    struct afs_wb_key, vnode_link);
698 		list_del(&wbk->vnode_link);
699 		afs_put_wb_key(wbk);
700 	}
701 
702 	fscache_relinquish_cookie(afs_vnode_cache(vnode),
703 				  test_bit(AFS_VNODE_DELETED, &vnode->flags));
704 
705 	afs_prune_wb_keys(vnode);
706 	afs_put_permits(rcu_access_pointer(vnode->permit_cache));
707 	key_put(vnode->silly_key);
708 	vnode->silly_key = NULL;
709 	key_put(vnode->lock_key);
710 	vnode->lock_key = NULL;
711 	_leave("");
712 }
713 
afs_setattr_success(struct afs_operation * op)714 static void afs_setattr_success(struct afs_operation *op)
715 {
716 	struct afs_vnode_param *vp = &op->file[0];
717 	struct inode *inode = &vp->vnode->netfs.inode;
718 	loff_t old_i_size = i_size_read(inode);
719 
720 	op->setattr.old_i_size = old_i_size;
721 	afs_vnode_commit_status(op, vp);
722 	/* inode->i_size has now been changed. */
723 
724 	if (op->setattr.attr->ia_valid & ATTR_SIZE) {
725 		loff_t size = op->setattr.attr->ia_size;
726 		if (size > old_i_size)
727 			pagecache_isize_extended(inode, old_i_size, size);
728 	}
729 }
730 
afs_setattr_edit_file(struct afs_operation * op)731 static void afs_setattr_edit_file(struct afs_operation *op)
732 {
733 	struct afs_vnode_param *vp = &op->file[0];
734 	struct afs_vnode *vnode = vp->vnode;
735 	struct inode *inode = &vnode->netfs.inode;
736 
737 	if (op->setattr.attr->ia_valid & ATTR_SIZE) {
738 		loff_t size = op->setattr.attr->ia_size;
739 		loff_t old = op->setattr.old_i_size;
740 
741 		/* Note: inode->i_size was updated by afs_apply_status() inside
742 		 * the I/O and callback locks.
743 		 */
744 
745 		if (size != old) {
746 			truncate_pagecache(inode, size);
747 			netfs_resize_file(&vnode->netfs, size, true);
748 			fscache_resize_cookie(afs_vnode_cache(vnode), size);
749 		}
750 	}
751 }
752 
753 static const struct afs_operation_ops afs_setattr_operation = {
754 	.issue_afs_rpc	= afs_fs_setattr,
755 	.issue_yfs_rpc	= yfs_fs_setattr,
756 	.success	= afs_setattr_success,
757 	.edit_dir	= afs_setattr_edit_file,
758 };
759 
760 /*
761  * set the attributes of an inode
762  */
afs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)763 int afs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
764 		struct iattr *attr)
765 {
766 	const unsigned int supported =
767 		ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
768 		ATTR_MTIME | ATTR_MTIME_SET | ATTR_TIMES_SET | ATTR_TOUCH;
769 	struct afs_operation *op;
770 	struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
771 	struct inode *inode = &vnode->netfs.inode;
772 	loff_t i_size;
773 	int ret;
774 
775 	_enter("{%llx:%llu},{n=%pd},%x",
776 	       vnode->fid.vid, vnode->fid.vnode, dentry,
777 	       attr->ia_valid);
778 
779 	if (!(attr->ia_valid & supported)) {
780 		_leave(" = 0 [unsupported]");
781 		return 0;
782 	}
783 
784 	i_size = i_size_read(inode);
785 	if (attr->ia_valid & ATTR_SIZE) {
786 		if (!S_ISREG(inode->i_mode))
787 			return -EISDIR;
788 
789 		ret = inode_newsize_ok(inode, attr->ia_size);
790 		if (ret)
791 			return ret;
792 
793 		if (attr->ia_size == i_size)
794 			attr->ia_valid &= ~ATTR_SIZE;
795 	}
796 
797 	fscache_use_cookie(afs_vnode_cache(vnode), true);
798 
799 	/* Prevent any new writebacks from starting whilst we do this. */
800 	down_write(&vnode->validate_lock);
801 
802 	if ((attr->ia_valid & ATTR_SIZE) && S_ISREG(inode->i_mode)) {
803 		loff_t size = attr->ia_size;
804 
805 		/* Wait for any outstanding writes to the server to complete */
806 		loff_t from = min(size, i_size);
807 		loff_t to = max(size, i_size);
808 		ret = filemap_fdatawait_range(inode->i_mapping, from, to);
809 		if (ret < 0)
810 			goto out_unlock;
811 
812 		/* Don't talk to the server if we're just shortening in-memory
813 		 * writes that haven't gone to the server yet.
814 		 */
815 		if (!(attr->ia_valid & (supported & ~ATTR_SIZE & ~ATTR_MTIME)) &&
816 		    attr->ia_size < i_size &&
817 		    attr->ia_size > netfs_read_remote_i_size(inode)) {
818 			truncate_setsize(inode, attr->ia_size);
819 			netfs_resize_file(&vnode->netfs, size, false);
820 			fscache_resize_cookie(afs_vnode_cache(vnode),
821 					      attr->ia_size);
822 			ret = 0;
823 			goto out_unlock;
824 		}
825 	}
826 
827 	op = afs_alloc_operation(((attr->ia_valid & ATTR_FILE) ?
828 				  afs_file_key(attr->ia_file) : NULL),
829 				 vnode->volume);
830 	if (IS_ERR(op)) {
831 		ret = PTR_ERR(op);
832 		goto out_unlock;
833 	}
834 
835 	afs_op_set_vnode(op, 0, vnode);
836 	op->setattr.attr = attr;
837 
838 	if (attr->ia_valid & ATTR_SIZE) {
839 		op->file[0].dv_delta = 1;
840 		op->file[0].set_size = true;
841 	}
842 	op->ctime = attr->ia_ctime;
843 	op->file[0].update_ctime = 1;
844 	op->file[0].modification = true;
845 
846 	op->ops = &afs_setattr_operation;
847 	ret = afs_do_sync_operation(op);
848 
849 out_unlock:
850 	up_write(&vnode->validate_lock);
851 	fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
852 	_leave(" = %d", ret);
853 	return ret;
854 }
855