1 /* AFS silly rename handling
3 * Copyright (C) 2019 Red Hat, Inc. All Rights Reserved.
5 * - Derived from NFS's sillyrename.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
13 #include <linux/kernel.h>
15 #include <linux/namei.h>
16 #include <linux/fsnotify.h>
20 * Actually perform the silly rename step.
22 static int afs_do_silly_rename(struct afs_vnode *dvnode, struct afs_vnode *vnode,
23 struct dentry *old, struct dentry *new,
26 struct afs_fs_cursor fc;
27 struct afs_status_cb *scb;
28 int ret = -ERESTARTSYS;
30 _enter("%pd,%pd", old, new);
32 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
36 trace_afs_silly_rename(vnode, false);
37 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
38 afs_dataversion_t dir_data_version = dvnode->status.data_version + 1;
40 while (afs_select_fileserver(&fc)) {
41 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
42 afs_fs_rename(&fc, old->d_name.name,
43 dvnode, new->d_name.name,
47 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
48 &dir_data_version, scb);
49 ret = afs_end_vnode_operation(&fc);
53 spin_lock(&old->d_lock);
54 old->d_flags |= DCACHE_NFSFS_RENAMED;
55 spin_unlock(&old->d_lock);
56 if (dvnode->silly_key != key) {
57 key_put(dvnode->silly_key);
58 dvnode->silly_key = key_get(key);
61 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
62 afs_edit_dir_remove(dvnode, &old->d_name,
63 afs_edit_dir_for_silly_0);
64 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
65 afs_edit_dir_add(dvnode, &new->d_name,
66 &vnode->fid, afs_edit_dir_for_silly_1);
68 /* vfs_unlink and the like do not issue this when a file is
69 * sillyrenamed, so do it here.
71 fsnotify_nameremove(old, 0);
80 * afs_sillyrename - Perform a silly-rename of a dentry
82 * AFS is stateless and the server doesn't know when the client is holding a
83 * file open. To prevent application problems when a file is unlinked while
84 * it's still open, the client performs a "silly-rename". That is, it renames
85 * the file to a hidden file in the same directory, and only performs the
86 * unlink once the last reference to it is put.
88 * The final cleanup is done during dentry_iput.
90 int afs_sillyrename(struct afs_vnode *dvnode, struct afs_vnode *vnode,
91 struct dentry *dentry, struct key *key)
93 static unsigned int sillycounter;
94 struct dentry *sdentry = NULL;
95 unsigned char silly[16];
100 /* We don't allow a dentry to be silly-renamed twice. */
101 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
111 /* Create a silly name. Note that the ".__afs" prefix is
112 * understood by the salvager and must not be changed.
114 slen = scnprintf(silly, sizeof(silly), ".__afs%04X", sillycounter);
115 sdentry = lookup_one_len(silly, dentry->d_parent, slen);
117 /* N.B. Better to return EBUSY here ... it could be dangerous
118 * to delete the file while it's in use.
122 } while (!d_is_negative(sdentry));
124 ihold(&vnode->vfs_inode);
126 ret = afs_do_silly_rename(dvnode, vnode, dentry, sdentry, key);
129 /* The rename succeeded. */
130 d_move(dentry, sdentry);
133 /* The result of the rename is unknown. Play it safe by forcing
140 iput(&vnode->vfs_inode);
143 _leave(" = %d", ret);
148 * Tell the server to remove a sillyrename file.
150 static int afs_do_silly_unlink(struct afs_vnode *dvnode, struct afs_vnode *vnode,
151 struct dentry *dentry, struct key *key)
153 struct afs_fs_cursor fc;
154 struct afs_status_cb *scb;
155 int ret = -ERESTARTSYS;
159 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
163 trace_afs_silly_rename(vnode, true);
164 if (afs_begin_vnode_operation(&fc, dvnode, key, false)) {
165 afs_dataversion_t dir_data_version = dvnode->status.data_version + 1;
167 while (afs_select_fileserver(&fc)) {
168 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
170 if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
171 !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
172 yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
174 if (fc.ac.error != -ECONNABORTED ||
175 fc.ac.abort_code != RXGEN_OPCODE)
177 set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
180 afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
183 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
184 &dir_data_version, &scb[0]);
185 ret = afs_end_vnode_operation(&fc);
187 drop_nlink(&vnode->vfs_inode);
188 if (vnode->vfs_inode.i_nlink == 0) {
189 set_bit(AFS_VNODE_DELETED, &vnode->flags);
190 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
194 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
195 afs_edit_dir_remove(dvnode, &dentry->d_name,
196 afs_edit_dir_for_unlink);
200 _leave(" = %d", ret);
205 * Remove sillyrename file on iput.
207 int afs_silly_iput(struct dentry *dentry, struct inode *inode)
209 struct afs_vnode *dvnode = AFS_FS_I(d_inode(dentry->d_parent));
210 struct afs_vnode *vnode = AFS_FS_I(inode);
211 struct dentry *alias;
214 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
216 _enter("%p{%pd},%llx", dentry, dentry, vnode->fid.vnode);
218 down_read(&dvnode->rmdir_lock);
220 alias = d_alloc_parallel(dentry->d_parent, &dentry->d_name, &wq);
222 up_read(&dvnode->rmdir_lock);
226 if (!d_in_lookup(alias)) {
227 /* We raced with lookup... See if we need to transfer the
228 * sillyrename information to the aliased dentry.
231 spin_lock(&alias->d_lock);
232 if (d_really_is_positive(alias) &&
233 !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
234 alias->d_flags |= DCACHE_NFSFS_RENAMED;
237 spin_unlock(&alias->d_lock);
238 up_read(&dvnode->rmdir_lock);
243 /* Stop lock-release from complaining. */
244 spin_lock(&vnode->lock);
245 vnode->lock_state = AFS_VNODE_LOCK_DELETED;
246 trace_afs_flock_ev(vnode, NULL, afs_flock_silly_delete, 0);
247 spin_unlock(&vnode->lock);
249 afs_do_silly_unlink(dvnode, vnode, dentry, dvnode->silly_key);
250 up_read(&dvnode->rmdir_lock);
251 d_lookup_done(alias);