2 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
4 * This software may be freely redistributed under the terms of the
5 * GNU General Public License.
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.
11 * Authors: David Woodhouse <dwmw2@infradead.org>
12 * David Howells <dhowells@redhat.com>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.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>
28 void afs_init_new_symlink(struct afs_vnode
*vnode
, struct afs_operation
*op
)
30 size_t size
= strlen(op
->create
.symlink
) + 1;
34 if (netfs_alloc_folioq_buffer(NULL
, &vnode
->directory
, &dsize
, size
,
35 mapping_gfp_mask(vnode
->netfs
.inode
.i_mapping
)) < 0)
38 vnode
->directory_size
= dsize
;
39 p
= kmap_local_folio(folioq_folio(vnode
->directory
, 0), 0);
40 memcpy(p
, op
->create
.symlink
, size
);
42 set_bit(AFS_VNODE_DIR_READ
, &vnode
->flags
);
43 netfs_single_mark_inode_dirty(&vnode
->netfs
.inode
);
46 static void afs_put_link(void *arg
)
48 struct folio
*folio
= virt_to_folio(arg
);
54 const char *afs_get_link(struct dentry
*dentry
, struct inode
*inode
,
55 struct delayed_call
*callback
)
57 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
64 if (!test_bit(AFS_VNODE_DIR_READ
, &vnode
->flags
) || !afs_check_validity(vnode
))
65 return ERR_PTR(-ECHILD
);
69 if (test_bit(AFS_VNODE_DIR_READ
, &vnode
->flags
))
72 ret
= afs_validate(vnode
, NULL
);
76 if (!test_and_clear_bit(AFS_VNODE_ZAP_DATA
, &vnode
->flags
) &&
77 test_bit(AFS_VNODE_DIR_READ
, &vnode
->flags
))
81 ret
= afs_read_single(vnode
, NULL
);
84 set_bit(AFS_VNODE_DIR_READ
, &vnode
->flags
);
87 folio
= folioq_folio(vnode
->directory
, 0);
89 content
= kmap_local_folio(folio
, 0);
90 set_delayed_call(callback
, afs_put_link
, content
);
94 int afs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
96 DEFINE_DELAYED_CALL(done
);
100 content
= afs_get_link(dentry
, d_inode(dentry
), &done
);
101 if (IS_ERR(content
)) {
102 do_delayed_call(&done
);
103 return PTR_ERR(content
);
106 len
= umin(strlen(content
), buflen
);
107 if (copy_to_user(buffer
, content
, len
))
109 do_delayed_call(&done
);
113 static const struct inode_operations afs_symlink_inode_operations
= {
114 .get_link
= afs_get_link
,
115 .readlink
= afs_readlink
,
118 static noinline
void dump_vnode(struct afs_vnode
*vnode
, struct afs_vnode
*parent_vnode
)
120 static unsigned long once_only
;
122 pr_warn("kAFS: AFS vnode with undefined type %u\n", vnode
->status
.type
);
123 pr_warn("kAFS: A=%d m=%o s=%llx v=%llx\n",
124 vnode
->status
.abort_code
,
127 vnode
->status
.data_version
);
128 pr_warn("kAFS: vnode %llx:%llx:%x\n",
133 pr_warn("kAFS: dir %llx:%llx:%x\n",
134 parent_vnode
->fid
.vid
,
135 parent_vnode
->fid
.vnode
,
136 parent_vnode
->fid
.unique
);
138 if (!test_and_set_bit(0, &once_only
))
143 * Set parameters for the netfs library
145 static void afs_set_netfs_context(struct afs_vnode
*vnode
)
147 netfs_inode_init(&vnode
->netfs
, &afs_req_ops
, true);
151 * Initialise an inode from the vnode status.
153 static int afs_inode_init_from_status(struct afs_operation
*op
,
154 struct afs_vnode_param
*vp
,
155 struct afs_vnode
*vnode
)
157 struct afs_file_status
*status
= &vp
->scb
.status
;
158 struct inode
*inode
= AFS_VNODE_TO_I(vnode
);
161 _enter("{%llx:%llu.%u} %s",
162 vp
->fid
.vid
, vp
->fid
.vnode
, vp
->fid
.unique
,
163 op
->type
? op
->type
->name
: "???");
165 _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
168 (unsigned long long) status
->size
,
169 status
->data_version
,
172 write_seqlock(&vnode
->cb_lock
);
174 vnode
->cb_v_check
= op
->cb_v_break
;
175 vnode
->status
= *status
;
177 t
= status
->mtime_client
;
178 inode_set_ctime_to_ts(inode
, t
);
179 inode_set_mtime_to_ts(inode
, t
);
180 inode_set_atime_to_ts(inode
, t
);
181 inode
->i_flags
|= S_NOATIME
;
182 inode
->i_uid
= make_kuid(&init_user_ns
, status
->owner
);
183 inode
->i_gid
= make_kgid(&init_user_ns
, status
->group
);
184 set_nlink(&vnode
->netfs
.inode
, status
->nlink
);
186 switch (status
->type
) {
188 inode
->i_mode
= S_IFREG
| (status
->mode
& S_IALLUGO
);
189 inode
->i_op
= &afs_file_inode_operations
;
190 inode
->i_fop
= &afs_file_operations
;
191 inode
->i_mapping
->a_ops
= &afs_file_aops
;
192 mapping_set_large_folios(inode
->i_mapping
);
195 inode
->i_mode
= S_IFDIR
| (status
->mode
& S_IALLUGO
);
196 inode
->i_op
= &afs_dir_inode_operations
;
197 inode
->i_fop
= &afs_dir_file_operations
;
198 inode
->i_mapping
->a_ops
= &afs_dir_aops
;
199 __set_bit(NETFS_ICTX_SINGLE_NO_UPLOAD
, &vnode
->netfs
.flags
);
200 /* Assume locally cached directory data will be valid. */
201 __set_bit(AFS_VNODE_DIR_VALID
, &vnode
->flags
);
203 case AFS_FTYPE_SYMLINK
:
204 /* Symlinks with a mode of 0644 are actually mountpoints. */
205 if ((status
->mode
& 0777) == 0644) {
206 inode
->i_flags
|= S_AUTOMOUNT
;
208 set_bit(AFS_VNODE_MOUNTPOINT
, &vnode
->flags
);
210 inode
->i_mode
= S_IFDIR
| 0555;
211 inode
->i_op
= &afs_mntpt_inode_operations
;
212 inode
->i_fop
= &afs_mntpt_file_operations
;
214 inode
->i_mode
= S_IFLNK
| status
->mode
;
215 inode
->i_op
= &afs_symlink_inode_operations
;
217 inode
->i_mapping
->a_ops
= &afs_dir_aops
;
218 inode_nohighmem(inode
);
219 mapping_set_release_always(inode
->i_mapping
);
222 dump_vnode(vnode
, op
->file
[0].vnode
!= vnode
? op
->file
[0].vnode
: NULL
);
223 write_sequnlock(&vnode
->cb_lock
);
224 return afs_protocol_error(NULL
, afs_eproto_file_type
);
227 afs_set_i_size(vnode
, status
->size
);
228 afs_set_netfs_context(vnode
);
230 vnode
->invalid_before
= status
->data_version
;
231 trace_afs_set_dv(vnode
, status
->data_version
);
232 inode_set_iversion_raw(&vnode
->netfs
.inode
, status
->data_version
);
234 if (!vp
->scb
.have_cb
) {
235 /* it's a symlink we just created (the fileserver
236 * didn't give us a callback) */
237 afs_clear_cb_promise(vnode
, afs_cb_promise_set_new_symlink
);
239 vnode
->cb_server
= op
->server
;
240 afs_set_cb_promise(vnode
, vp
->scb
.callback
.expires_at
,
241 afs_cb_promise_set_new_inode
);
244 write_sequnlock(&vnode
->cb_lock
);
249 * Update the core inode struct from a returned status record.
251 static void afs_apply_status(struct afs_operation
*op
,
252 struct afs_vnode_param
*vp
)
254 struct afs_file_status
*status
= &vp
->scb
.status
;
255 struct afs_vnode
*vnode
= vp
->vnode
;
256 struct inode
*inode
= &vnode
->netfs
.inode
;
259 bool unexpected_jump
= false;
260 bool data_changed
= false;
261 bool change_size
= vp
->set_size
;
263 _enter("{%llx:%llu.%u} %s",
264 vp
->fid
.vid
, vp
->fid
.vnode
, vp
->fid
.unique
,
265 op
->type
? op
->type
->name
: "???");
267 BUG_ON(test_bit(AFS_VNODE_UNSET
, &vnode
->flags
));
269 if (status
->type
!= vnode
->status
.type
) {
270 pr_warn("Vnode %llx:%llx:%x changed type %u to %u\n",
274 status
->type
, vnode
->status
.type
);
275 afs_protocol_error(NULL
, afs_eproto_bad_status
);
279 if (status
->nlink
!= vnode
->status
.nlink
)
280 set_nlink(inode
, status
->nlink
);
282 if (status
->owner
!= vnode
->status
.owner
)
283 inode
->i_uid
= make_kuid(&init_user_ns
, status
->owner
);
285 if (status
->group
!= vnode
->status
.group
)
286 inode
->i_gid
= make_kgid(&init_user_ns
, status
->group
);
288 if (status
->mode
!= vnode
->status
.mode
) {
289 mode
= inode
->i_mode
;
291 mode
|= status
->mode
& S_IALLUGO
;
292 WRITE_ONCE(inode
->i_mode
, mode
);
295 t
= status
->mtime_client
;
296 inode_set_mtime_to_ts(inode
, t
);
297 if (vp
->update_ctime
)
298 inode_set_ctime_to_ts(inode
, op
->ctime
);
300 if (vnode
->status
.data_version
!= status
->data_version
) {
301 trace_afs_set_dv(vnode
, status
->data_version
);
305 vnode
->status
= *status
;
307 if (vp
->dv_before
+ vp
->dv_delta
!= status
->data_version
) {
308 trace_afs_dv_mismatch(vnode
, vp
->dv_before
, vp
->dv_delta
,
309 status
->data_version
);
311 if (vnode
->cb_ro_snapshot
== atomic_read(&vnode
->volume
->cb_ro_snapshot
) &&
312 atomic64_read(&vnode
->cb_expires_at
) != AFS_NO_CB_PROMISE
)
313 pr_warn("kAFS: vnode modified {%llx:%llu} %llx->%llx %s (op=%x)\n",
314 vnode
->fid
.vid
, vnode
->fid
.vnode
,
315 (unsigned long long)vp
->dv_before
+ vp
->dv_delta
,
316 (unsigned long long)status
->data_version
,
317 op
->type
? op
->type
->name
: "???",
320 vnode
->invalid_before
= status
->data_version
;
321 if (vnode
->status
.type
== AFS_FTYPE_DIR
)
322 afs_invalidate_dir(vnode
, afs_dir_invalid_dv_mismatch
);
324 set_bit(AFS_VNODE_ZAP_DATA
, &vnode
->flags
);
327 unexpected_jump
= true;
328 } else if (vnode
->status
.type
== AFS_FTYPE_DIR
) {
329 /* Expected directory change is handled elsewhere so
330 * that we can locally edit the directory and save on a
333 if (test_bit(AFS_VNODE_DIR_VALID
, &vnode
->flags
))
334 data_changed
= false;
339 inode_set_iversion_raw(inode
, status
->data_version
);
341 /* Only update the size if the data version jumped. If the
342 * file is being modified locally, then we might have our own
343 * idea of what the size should be that's not the same as
344 * what's on the server.
346 vnode
->netfs
.remote_i_size
= status
->size
;
347 if (change_size
|| status
->size
> i_size_read(inode
)) {
348 afs_set_i_size(vnode
, status
->size
);
350 vnode
->netfs
.zero_point
= status
->size
;
351 inode_set_ctime_to_ts(inode
, t
);
352 inode_set_atime_to_ts(inode
, t
);
354 if (op
->ops
== &afs_fetch_data_operation
)
355 op
->fetch
.subreq
->rreq
->i_size
= status
->size
;
360 * Apply a callback to a vnode.
362 static void afs_apply_callback(struct afs_operation
*op
,
363 struct afs_vnode_param
*vp
)
365 struct afs_callback
*cb
= &vp
->scb
.callback
;
366 struct afs_vnode
*vnode
= vp
->vnode
;
368 if (!afs_cb_is_broken(vp
->cb_break_before
, vnode
)) {
369 if (op
->volume
->type
== AFSVL_RWVOL
)
370 vnode
->cb_server
= op
->server
;
371 afs_set_cb_promise(vnode
, cb
->expires_at
, afs_cb_promise_set_apply_cb
);
376 * Apply the received status and callback to an inode all in the same critical
377 * section to avoid races with afs_validate().
379 void afs_vnode_commit_status(struct afs_operation
*op
, struct afs_vnode_param
*vp
)
381 struct afs_vnode
*vnode
= vp
->vnode
;
385 write_seqlock(&vnode
->cb_lock
);
387 if (vp
->scb
.have_error
) {
388 /* A YFS server will return this from RemoveFile2 and AFS and
389 * YFS will return this from InlineBulkStatus.
391 if (vp
->scb
.status
.abort_code
== VNOVNODE
) {
392 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
393 clear_nlink(&vnode
->netfs
.inode
);
394 __afs_break_callback(vnode
, afs_cb_break_for_deleted
);
395 op
->flags
&= ~AFS_OPERATION_DIR_CONFLICT
;
397 } else if (vp
->scb
.have_status
) {
398 if (vp
->speculative
&&
399 (test_bit(AFS_VNODE_MODIFYING
, &vnode
->flags
) ||
400 vp
->dv_before
!= vnode
->status
.data_version
))
401 /* Ignore the result of a speculative bulk status fetch
402 * if it splits around a modification op, thereby
403 * appearing to regress the data version.
406 afs_apply_status(op
, vp
);
408 afs_apply_callback(op
, vp
);
409 } else if (vp
->op_unlinked
&& !(op
->flags
& AFS_OPERATION_DIR_CONFLICT
)) {
410 drop_nlink(&vnode
->netfs
.inode
);
411 if (vnode
->netfs
.inode
.i_nlink
== 0) {
412 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
413 __afs_break_callback(vnode
, afs_cb_break_for_deleted
);
418 write_sequnlock(&vnode
->cb_lock
);
420 if (vp
->scb
.have_status
)
421 afs_cache_permit(vnode
, op
->key
, vp
->cb_break_before
, &vp
->scb
);
424 static void afs_fetch_status_success(struct afs_operation
*op
)
426 struct afs_vnode_param
*vp
= &op
->file
[op
->fetch_status
.which
];
427 struct afs_vnode
*vnode
= vp
->vnode
;
430 if (vnode
->netfs
.inode
.i_state
& I_NEW
) {
431 ret
= afs_inode_init_from_status(op
, vp
, vnode
);
432 afs_op_set_error(op
, ret
);
434 afs_cache_permit(vnode
, op
->key
, vp
->cb_break_before
, &vp
->scb
);
436 afs_vnode_commit_status(op
, vp
);
440 const struct afs_operation_ops afs_fetch_status_operation
= {
441 .issue_afs_rpc
= afs_fs_fetch_status
,
442 .issue_yfs_rpc
= yfs_fs_fetch_status
,
443 .success
= afs_fetch_status_success
,
444 .aborted
= afs_check_for_remote_deletion
,
448 * Fetch file status from the volume.
450 int afs_fetch_status(struct afs_vnode
*vnode
, struct key
*key
, bool is_new
,
451 afs_access_t
*_caller_access
)
453 struct afs_operation
*op
;
455 _enter("%s,{%llx:%llu.%u,S=%lx}",
457 vnode
->fid
.vid
, vnode
->fid
.vnode
, vnode
->fid
.unique
,
460 op
= afs_alloc_operation(key
, vnode
->volume
);
464 afs_op_set_vnode(op
, 0, vnode
);
467 op
->ops
= &afs_fetch_status_operation
;
468 afs_begin_vnode_operation(op
);
469 afs_wait_for_operation(op
);
472 *_caller_access
= op
->file
[0].scb
.status
.caller_access
;
473 return afs_put_operation(op
);
477 * ilookup() comparator
479 int afs_ilookup5_test_by_fid(struct inode
*inode
, void *opaque
)
481 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
482 struct afs_fid
*fid
= opaque
;
484 return (fid
->vnode
== vnode
->fid
.vnode
&&
485 fid
->vnode_hi
== vnode
->fid
.vnode_hi
&&
486 fid
->unique
== vnode
->fid
.unique
);
492 static int afs_iget5_test(struct inode
*inode
, void *opaque
)
494 struct afs_vnode_param
*vp
= opaque
;
495 //struct afs_vnode *vnode = AFS_FS_I(inode);
497 return afs_ilookup5_test_by_fid(inode
, &vp
->fid
);
501 * iget5() inode initialiser
503 static int afs_iget5_set(struct inode
*inode
, void *opaque
)
505 struct afs_vnode_param
*vp
= opaque
;
506 struct afs_super_info
*as
= AFS_FS_S(inode
->i_sb
);
507 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
509 vnode
->volume
= as
->volume
;
510 vnode
->fid
= vp
->fid
;
512 /* YFS supports 96-bit vnode IDs, but Linux only supports
513 * 64-bit inode numbers.
515 inode
->i_ino
= vnode
->fid
.vnode
;
516 inode
->i_generation
= vnode
->fid
.unique
;
521 * Get a cache cookie for an inode.
523 static void afs_get_inode_cache(struct afs_vnode
*vnode
)
525 #ifdef CONFIG_AFS_FSCACHE
529 __be32 vnode_id_ext
[2]; /* Allow for a 96-bit key */
531 struct afs_vnode_cache_aux aux
;
533 if (vnode
->status
.type
!= AFS_FTYPE_FILE
&&
534 vnode
->status
.type
!= AFS_FTYPE_DIR
&&
535 vnode
->status
.type
!= AFS_FTYPE_SYMLINK
) {
536 vnode
->netfs
.cache
= NULL
;
540 key
.vnode_id
= htonl(vnode
->fid
.vnode
);
541 key
.unique
= htonl(vnode
->fid
.unique
);
542 key
.vnode_id_ext
[0] = htonl(vnode
->fid
.vnode
>> 32);
543 key
.vnode_id_ext
[1] = htonl(vnode
->fid
.vnode_hi
);
544 afs_set_cache_aux(vnode
, &aux
);
546 afs_vnode_set_cache(vnode
,
547 fscache_acquire_cookie(
548 vnode
->volume
->cache
,
549 vnode
->status
.type
== AFS_FTYPE_FILE
?
550 0 : FSCACHE_ADV_SINGLE_CHUNK
,
553 i_size_read(&vnode
->netfs
.inode
)));
560 struct inode
*afs_iget(struct afs_operation
*op
, struct afs_vnode_param
*vp
)
562 struct afs_vnode_param
*dvp
= &op
->file
[0];
563 struct super_block
*sb
= dvp
->vnode
->netfs
.inode
.i_sb
;
564 struct afs_vnode
*vnode
;
568 _enter(",{%llx:%llu.%u},,", vp
->fid
.vid
, vp
->fid
.vnode
, vp
->fid
.unique
);
570 inode
= iget5_locked(sb
, vp
->fid
.vnode
, afs_iget5_test
, afs_iget5_set
, vp
);
572 _leave(" = -ENOMEM");
573 return ERR_PTR(-ENOMEM
);
576 vnode
= AFS_FS_I(inode
);
578 _debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }",
579 inode
, vnode
->fid
.vid
, vnode
->fid
.vnode
, vnode
->fid
.unique
);
581 /* deal with an existing inode */
582 if (!(inode
->i_state
& I_NEW
)) {
583 _leave(" = %p", inode
);
587 ret
= afs_inode_init_from_status(op
, vp
, vnode
);
591 afs_get_inode_cache(vnode
);
594 clear_bit(AFS_VNODE_UNSET
, &vnode
->flags
);
595 unlock_new_inode(inode
);
596 _leave(" = %p", inode
);
602 _leave(" = %d [bad]", ret
);
606 static int afs_iget5_set_root(struct inode
*inode
, void *opaque
)
608 struct afs_super_info
*as
= AFS_FS_S(inode
->i_sb
);
609 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
611 vnode
->volume
= as
->volume
;
612 vnode
->fid
.vid
= as
->volume
->vid
;
613 vnode
->fid
.vnode
= 1;
614 vnode
->fid
.unique
= 1;
616 inode
->i_generation
= 1;
621 * Set up the root inode for a volume. This is always vnode 1, unique 1 within
624 struct inode
*afs_root_iget(struct super_block
*sb
, struct key
*key
)
626 struct afs_super_info
*as
= AFS_FS_S(sb
);
627 struct afs_operation
*op
;
628 struct afs_vnode
*vnode
;
632 _enter(",{%llx},,", as
->volume
->vid
);
634 inode
= iget5_locked(sb
, 1, NULL
, afs_iget5_set_root
, NULL
);
636 _leave(" = -ENOMEM");
637 return ERR_PTR(-ENOMEM
);
640 _debug("GOT ROOT INODE %p { vl=%llx }", inode
, as
->volume
->vid
);
642 BUG_ON(!(inode
->i_state
& I_NEW
));
644 vnode
= AFS_FS_I(inode
);
645 vnode
->cb_v_check
= atomic_read(&as
->volume
->cb_v_break
);
646 afs_set_netfs_context(vnode
);
648 op
= afs_alloc_operation(key
, as
->volume
);
654 afs_op_set_vnode(op
, 0, vnode
);
657 op
->ops
= &afs_fetch_status_operation
;
658 ret
= afs_do_sync_operation(op
);
662 afs_get_inode_cache(vnode
);
664 clear_bit(AFS_VNODE_UNSET
, &vnode
->flags
);
665 unlock_new_inode(inode
);
666 _leave(" = %p", inode
);
671 _leave(" = %d [bad]", ret
);
676 * read the attributes of an inode
678 int afs_getattr(struct mnt_idmap
*idmap
, const struct path
*path
,
679 struct kstat
*stat
, u32 request_mask
, unsigned int query_flags
)
681 struct inode
*inode
= d_inode(path
->dentry
);
682 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
686 _enter("{ ino=%lu v=%u }", inode
->i_ino
, inode
->i_generation
);
689 !(query_flags
& AT_STATX_DONT_SYNC
) &&
690 atomic64_read(&vnode
->cb_expires_at
) == AFS_NO_CB_PROMISE
) {
691 key
= afs_request_key(vnode
->volume
->cell
);
694 ret
= afs_validate(vnode
, key
);
701 seq
= read_seqbegin(&vnode
->cb_lock
);
702 generic_fillattr(&nop_mnt_idmap
, request_mask
, inode
, stat
);
703 if (test_bit(AFS_VNODE_SILLY_DELETED
, &vnode
->flags
) &&
707 /* Lie about the size of directories. We maintain a locally
708 * edited copy and may make different allocation decisions on
709 * it, but we need to give userspace the server's size.
711 if (S_ISDIR(inode
->i_mode
))
712 stat
->size
= vnode
->netfs
.remote_i_size
;
713 } while (read_seqretry(&vnode
->cb_lock
, seq
));
719 * discard an AFS inode
721 int afs_drop_inode(struct inode
*inode
)
725 if (test_bit(AFS_VNODE_PSEUDODIR
, &AFS_FS_I(inode
)->flags
))
726 return generic_delete_inode(inode
);
728 return generic_drop_inode(inode
);
734 void afs_evict_inode(struct inode
*inode
)
736 struct afs_vnode_cache_aux aux
;
737 struct afs_super_info
*sbi
= AFS_FS_S(inode
->i_sb
);
738 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
740 _enter("{%llx:%llu.%d}",
745 _debug("CLEAR INODE %p", inode
);
747 ASSERTCMP(inode
->i_ino
, ==, vnode
->fid
.vnode
);
749 if ((S_ISDIR(inode
->i_mode
) ||
750 S_ISLNK(inode
->i_mode
)) &&
751 (inode
->i_state
& I_DIRTY
) &&
753 struct writeback_control wbc
= {
754 .sync_mode
= WB_SYNC_ALL
,
756 .range_end
= LLONG_MAX
,
759 afs_single_writepages(inode
->i_mapping
, &wbc
);
762 netfs_wait_for_outstanding_io(inode
);
763 truncate_inode_pages_final(&inode
->i_data
);
764 netfs_free_folioq_buffer(vnode
->directory
);
766 afs_set_cache_aux(vnode
, &aux
);
767 netfs_clear_inode_writeback(inode
, &aux
);
770 while (!list_empty(&vnode
->wb_keys
)) {
771 struct afs_wb_key
*wbk
= list_entry(vnode
->wb_keys
.next
,
772 struct afs_wb_key
, vnode_link
);
773 list_del(&wbk
->vnode_link
);
777 fscache_relinquish_cookie(afs_vnode_cache(vnode
),
778 test_bit(AFS_VNODE_DELETED
, &vnode
->flags
));
780 afs_prune_wb_keys(vnode
);
781 afs_put_permits(rcu_access_pointer(vnode
->permit_cache
));
782 key_put(vnode
->silly_key
);
783 vnode
->silly_key
= NULL
;
784 key_put(vnode
->lock_key
);
785 vnode
->lock_key
= NULL
;
789 static void afs_setattr_success(struct afs_operation
*op
)
791 struct afs_vnode_param
*vp
= &op
->file
[0];
792 struct inode
*inode
= &vp
->vnode
->netfs
.inode
;
793 loff_t old_i_size
= i_size_read(inode
);
795 op
->setattr
.old_i_size
= old_i_size
;
796 afs_vnode_commit_status(op
, vp
);
797 /* inode->i_size has now been changed. */
799 if (op
->setattr
.attr
->ia_valid
& ATTR_SIZE
) {
800 loff_t size
= op
->setattr
.attr
->ia_size
;
801 if (size
> old_i_size
)
802 pagecache_isize_extended(inode
, old_i_size
, size
);
806 static void afs_setattr_edit_file(struct afs_operation
*op
)
808 struct afs_vnode_param
*vp
= &op
->file
[0];
809 struct afs_vnode
*vnode
= vp
->vnode
;
810 struct inode
*inode
= &vnode
->netfs
.inode
;
812 if (op
->setattr
.attr
->ia_valid
& ATTR_SIZE
) {
813 loff_t size
= op
->setattr
.attr
->ia_size
;
814 loff_t old
= op
->setattr
.old_i_size
;
816 /* Note: inode->i_size was updated by afs_apply_status() inside
817 * the I/O and callback locks.
821 truncate_pagecache(inode
, size
);
822 netfs_resize_file(&vnode
->netfs
, size
, true);
823 fscache_resize_cookie(afs_vnode_cache(vnode
), size
);
828 static const struct afs_operation_ops afs_setattr_operation
= {
829 .issue_afs_rpc
= afs_fs_setattr
,
830 .issue_yfs_rpc
= yfs_fs_setattr
,
831 .success
= afs_setattr_success
,
832 .edit_dir
= afs_setattr_edit_file
,
836 * set the attributes of an inode
838 int afs_setattr(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
841 const unsigned int supported
=
842 ATTR_SIZE
| ATTR_MODE
| ATTR_UID
| ATTR_GID
|
843 ATTR_MTIME
| ATTR_MTIME_SET
| ATTR_TIMES_SET
| ATTR_TOUCH
;
844 struct afs_operation
*op
;
845 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
846 struct inode
*inode
= &vnode
->netfs
.inode
;
850 _enter("{%llx:%llu},{n=%pd},%x",
851 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
,
854 if (!(attr
->ia_valid
& supported
)) {
855 _leave(" = 0 [unsupported]");
859 i_size
= i_size_read(inode
);
860 if (attr
->ia_valid
& ATTR_SIZE
) {
861 if (!S_ISREG(inode
->i_mode
))
864 ret
= inode_newsize_ok(inode
, attr
->ia_size
);
868 if (attr
->ia_size
== i_size
)
869 attr
->ia_valid
&= ~ATTR_SIZE
;
872 fscache_use_cookie(afs_vnode_cache(vnode
), true);
874 /* Prevent any new writebacks from starting whilst we do this. */
875 down_write(&vnode
->validate_lock
);
877 if ((attr
->ia_valid
& ATTR_SIZE
) && S_ISREG(inode
->i_mode
)) {
878 loff_t size
= attr
->ia_size
;
880 /* Wait for any outstanding writes to the server to complete */
881 loff_t from
= min(size
, i_size
);
882 loff_t to
= max(size
, i_size
);
883 ret
= filemap_fdatawait_range(inode
->i_mapping
, from
, to
);
887 /* Don't talk to the server if we're just shortening in-memory
888 * writes that haven't gone to the server yet.
890 if (!(attr
->ia_valid
& (supported
& ~ATTR_SIZE
& ~ATTR_MTIME
)) &&
891 attr
->ia_size
< i_size
&&
892 attr
->ia_size
> vnode
->netfs
.remote_i_size
) {
893 truncate_setsize(inode
, attr
->ia_size
);
894 netfs_resize_file(&vnode
->netfs
, size
, false);
895 fscache_resize_cookie(afs_vnode_cache(vnode
),
902 op
= afs_alloc_operation(((attr
->ia_valid
& ATTR_FILE
) ?
903 afs_file_key(attr
->ia_file
) : NULL
),
910 afs_op_set_vnode(op
, 0, vnode
);
911 op
->setattr
.attr
= attr
;
913 if (attr
->ia_valid
& ATTR_SIZE
) {
914 op
->file
[0].dv_delta
= 1;
915 op
->file
[0].set_size
= true;
917 op
->ctime
= attr
->ia_ctime
;
918 op
->file
[0].update_ctime
= 1;
919 op
->file
[0].modification
= true;
921 op
->ops
= &afs_setattr_operation
;
922 ret
= afs_do_sync_operation(op
);
925 up_write(&vnode
->validate_lock
);
926 fscache_unuse_cookie(afs_vnode_cache(vnode
), NULL
, NULL
);
927 _leave(" = %d", ret
);