1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS silly rename handling
4 * Copyright (C) 2019 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 * - Derived from NFS's sillyrename.
9 #include <linux/kernel.h>
11 #include <linux/namei.h>
12 #include <linux/fsnotify.h>
16 * Actually perform the silly rename step.
18 static int afs_do_silly_rename(struct afs_vnode
*dvnode
, struct afs_vnode
*vnode
,
19 struct dentry
*old
, struct dentry
*new,
22 struct afs_fs_cursor fc
;
23 struct afs_status_cb
*scb
;
24 int ret
= -ERESTARTSYS
;
26 _enter("%pd,%pd", old
, new);
28 scb
= kzalloc(sizeof(struct afs_status_cb
), GFP_KERNEL
);
32 trace_afs_silly_rename(vnode
, false);
33 if (afs_begin_vnode_operation(&fc
, dvnode
, key
, true)) {
34 afs_dataversion_t dir_data_version
= dvnode
->status
.data_version
+ 1;
36 while (afs_select_fileserver(&fc
)) {
37 fc
.cb_break
= afs_calc_vnode_cb_break(dvnode
);
38 afs_fs_rename(&fc
, old
->d_name
.name
,
39 dvnode
, new->d_name
.name
,
43 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
,
44 &dir_data_version
, scb
);
45 ret
= afs_end_vnode_operation(&fc
);
49 spin_lock(&old
->d_lock
);
50 old
->d_flags
|= DCACHE_NFSFS_RENAMED
;
51 spin_unlock(&old
->d_lock
);
52 if (dvnode
->silly_key
!= key
) {
53 key_put(dvnode
->silly_key
);
54 dvnode
->silly_key
= key_get(key
);
57 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
))
58 afs_edit_dir_remove(dvnode
, &old
->d_name
,
59 afs_edit_dir_for_silly_0
);
60 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
))
61 afs_edit_dir_add(dvnode
, &new->d_name
,
62 &vnode
->fid
, afs_edit_dir_for_silly_1
);
71 * afs_sillyrename - Perform a silly-rename of a dentry
73 * AFS is stateless and the server doesn't know when the client is holding a
74 * file open. To prevent application problems when a file is unlinked while
75 * it's still open, the client performs a "silly-rename". That is, it renames
76 * the file to a hidden file in the same directory, and only performs the
77 * unlink once the last reference to it is put.
79 * The final cleanup is done during dentry_iput.
81 int afs_sillyrename(struct afs_vnode
*dvnode
, struct afs_vnode
*vnode
,
82 struct dentry
*dentry
, struct key
*key
)
84 static unsigned int sillycounter
;
85 struct dentry
*sdentry
= NULL
;
86 unsigned char silly
[16];
91 /* We don't allow a dentry to be silly-renamed twice. */
92 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
102 /* Create a silly name. Note that the ".__afs" prefix is
103 * understood by the salvager and must not be changed.
105 slen
= scnprintf(silly
, sizeof(silly
), ".__afs%04X", sillycounter
);
106 sdentry
= lookup_one_len(silly
, dentry
->d_parent
, slen
);
108 /* N.B. Better to return EBUSY here ... it could be dangerous
109 * to delete the file while it's in use.
113 } while (!d_is_negative(sdentry
));
115 ihold(&vnode
->vfs_inode
);
117 ret
= afs_do_silly_rename(dvnode
, vnode
, dentry
, sdentry
, key
);
120 /* The rename succeeded. */
121 d_move(dentry
, sdentry
);
124 /* The result of the rename is unknown. Play it safe by forcing
131 iput(&vnode
->vfs_inode
);
134 _leave(" = %d", ret
);
139 * Tell the server to remove a sillyrename file.
141 static int afs_do_silly_unlink(struct afs_vnode
*dvnode
, struct afs_vnode
*vnode
,
142 struct dentry
*dentry
, struct key
*key
)
144 struct afs_fs_cursor fc
;
145 struct afs_status_cb
*scb
;
146 int ret
= -ERESTARTSYS
;
150 scb
= kcalloc(2, sizeof(struct afs_status_cb
), GFP_KERNEL
);
154 trace_afs_silly_rename(vnode
, true);
155 if (afs_begin_vnode_operation(&fc
, dvnode
, key
, false)) {
156 afs_dataversion_t dir_data_version
= dvnode
->status
.data_version
+ 1;
158 while (afs_select_fileserver(&fc
)) {
159 fc
.cb_break
= afs_calc_vnode_cb_break(dvnode
);
161 if (test_bit(AFS_SERVER_FL_IS_YFS
, &fc
.cbi
->server
->flags
) &&
162 !test_bit(AFS_SERVER_FL_NO_RM2
, &fc
.cbi
->server
->flags
)) {
163 yfs_fs_remove_file2(&fc
, vnode
, dentry
->d_name
.name
,
165 if (fc
.ac
.error
!= -ECONNABORTED
||
166 fc
.ac
.abort_code
!= RXGEN_OPCODE
)
168 set_bit(AFS_SERVER_FL_NO_RM2
, &fc
.cbi
->server
->flags
);
171 afs_fs_remove(&fc
, vnode
, dentry
->d_name
.name
, false, &scb
[0]);
174 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
,
175 &dir_data_version
, &scb
[0]);
176 ret
= afs_end_vnode_operation(&fc
);
178 drop_nlink(&vnode
->vfs_inode
);
179 if (vnode
->vfs_inode
.i_nlink
== 0) {
180 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
181 clear_bit(AFS_VNODE_CB_PROMISED
, &vnode
->flags
);
185 test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
))
186 afs_edit_dir_remove(dvnode
, &dentry
->d_name
,
187 afs_edit_dir_for_unlink
);
191 _leave(" = %d", ret
);
196 * Remove sillyrename file on iput.
198 int afs_silly_iput(struct dentry
*dentry
, struct inode
*inode
)
200 struct afs_vnode
*dvnode
= AFS_FS_I(d_inode(dentry
->d_parent
));
201 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
202 struct dentry
*alias
;
205 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
207 _enter("%p{%pd},%llx", dentry
, dentry
, vnode
->fid
.vnode
);
209 down_read(&dvnode
->rmdir_lock
);
211 alias
= d_alloc_parallel(dentry
->d_parent
, &dentry
->d_name
, &wq
);
213 up_read(&dvnode
->rmdir_lock
);
217 if (!d_in_lookup(alias
)) {
218 /* We raced with lookup... See if we need to transfer the
219 * sillyrename information to the aliased dentry.
222 spin_lock(&alias
->d_lock
);
223 if (d_really_is_positive(alias
) &&
224 !(alias
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
225 alias
->d_flags
|= DCACHE_NFSFS_RENAMED
;
228 spin_unlock(&alias
->d_lock
);
229 up_read(&dvnode
->rmdir_lock
);
234 /* Stop lock-release from complaining. */
235 spin_lock(&vnode
->lock
);
236 vnode
->lock_state
= AFS_VNODE_LOCK_DELETED
;
237 trace_afs_flock_ev(vnode
, NULL
, afs_flock_silly_delete
, 0);
238 spin_unlock(&vnode
->lock
);
240 afs_do_silly_unlink(dvnode
, vnode
, dentry
, dvnode
->silly_key
);
241 up_read(&dvnode
->rmdir_lock
);
242 d_lookup_done(alias
);