xref: /openbmc/linux/fs/afs/dir_silly.c (revision 752beb5e)
1 /* AFS silly rename handling
2  *
3  * Copyright (C) 2019 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  * - Derived from NFS's sillyrename.
6  *
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.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/fs.h>
15 #include <linux/namei.h>
16 #include <linux/fsnotify.h>
17 #include "internal.h"
18 
19 /*
20  * Actually perform the silly rename step.
21  */
22 static int afs_do_silly_rename(struct afs_vnode *dvnode, struct afs_vnode *vnode,
23 			       struct dentry *old, struct dentry *new,
24 			       struct key *key)
25 {
26 	struct afs_fs_cursor fc;
27 	u64 dir_data_version = dvnode->status.data_version;
28 	int ret = -ERESTARTSYS;
29 
30 	_enter("%pd,%pd", old, new);
31 
32 	trace_afs_silly_rename(vnode, false);
33 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
34 		while (afs_select_fileserver(&fc)) {
35 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
36 			afs_fs_rename(&fc, old->d_name.name,
37 				      dvnode, new->d_name.name,
38 				      dir_data_version, dir_data_version);
39 		}
40 
41 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
42 		ret = afs_end_vnode_operation(&fc);
43 	}
44 
45 	if (ret == 0) {
46 		spin_lock(&old->d_lock);
47 		old->d_flags |= DCACHE_NFSFS_RENAMED;
48 		spin_unlock(&old->d_lock);
49 		if (dvnode->silly_key != key) {
50 			key_put(dvnode->silly_key);
51 			dvnode->silly_key = key_get(key);
52 		}
53 
54 		if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
55 			afs_edit_dir_remove(dvnode, &old->d_name,
56 					    afs_edit_dir_for_silly_0);
57 		if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
58 			afs_edit_dir_add(dvnode, &new->d_name,
59 					 &vnode->fid, afs_edit_dir_for_silly_1);
60 
61 		/* vfs_unlink and the like do not issue this when a file is
62 		 * sillyrenamed, so do it here.
63 		 */
64 		fsnotify_nameremove(old, 0);
65 	}
66 
67 	_leave(" = %d", ret);
68 	return ret;
69 }
70 
71 /**
72  * afs_sillyrename - Perform a silly-rename of a dentry
73  *
74  * AFS is stateless and the server doesn't know when the client is holding a
75  * file open.  To prevent application problems when a file is unlinked while
76  * it's still open, the client performs a "silly-rename".  That is, it renames
77  * the file to a hidden file in the same directory, and only performs the
78  * unlink once the last reference to it is put.
79  *
80  * The final cleanup is done during dentry_iput.
81  */
82 int afs_sillyrename(struct afs_vnode *dvnode, struct afs_vnode *vnode,
83 		    struct dentry *dentry, struct key *key)
84 {
85 	static unsigned int sillycounter;
86 	struct dentry *sdentry = NULL;
87 	unsigned char silly[16];
88 	int ret = -EBUSY;
89 
90 	_enter("");
91 
92 	/* We don't allow a dentry to be silly-renamed twice. */
93 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
94 		return -EBUSY;
95 
96 	sdentry = NULL;
97 	do {
98 		int slen;
99 
100 		dput(sdentry);
101 		sillycounter++;
102 
103 		/* Create a silly name.  Note that the ".__afs" prefix is
104 		 * understood by the salvager and must not be changed.
105 		 */
106 		slen = scnprintf(silly, sizeof(silly), ".__afs%04X", sillycounter);
107 		sdentry = lookup_one_len(silly, dentry->d_parent, slen);
108 
109 		/* N.B. Better to return EBUSY here ... it could be dangerous
110 		 * to delete the file while it's in use.
111 		 */
112 		if (IS_ERR(sdentry))
113 			goto out;
114 	} while (!d_is_negative(sdentry));
115 
116 	ihold(&vnode->vfs_inode);
117 
118 	ret = afs_do_silly_rename(dvnode, vnode, dentry, sdentry, key);
119 	switch (ret) {
120 	case 0:
121 		/* The rename succeeded. */
122 		d_move(dentry, sdentry);
123 		break;
124 	case -ERESTARTSYS:
125 		/* The result of the rename is unknown. Play it safe by forcing
126 		 * a new lookup.
127 		 */
128 		d_drop(dentry);
129 		d_drop(sdentry);
130 	}
131 
132 	iput(&vnode->vfs_inode);
133 	dput(sdentry);
134 out:
135 	_leave(" = %d", ret);
136 	return ret;
137 }
138 
139 /*
140  * Tell the server to remove a sillyrename file.
141  */
142 static int afs_do_silly_unlink(struct afs_vnode *dvnode, struct afs_vnode *vnode,
143 			       struct dentry *dentry, struct key *key)
144 {
145 	struct afs_fs_cursor fc;
146 	u64 dir_data_version = dvnode->status.data_version;
147 	int ret = -ERESTARTSYS;
148 
149 	_enter("");
150 
151 	trace_afs_silly_rename(vnode, true);
152 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
153 		while (afs_select_fileserver(&fc)) {
154 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
155 
156 			if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
157 			    !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
158 				yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
159 						    dir_data_version);
160 				if (fc.ac.error != -ECONNABORTED ||
161 				    fc.ac.abort_code != RXGEN_OPCODE)
162 					continue;
163 				set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
164 			}
165 
166 			afs_fs_remove(&fc, vnode, dentry->d_name.name, false,
167 				      dir_data_version);
168 		}
169 
170 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
171 		ret = afs_end_vnode_operation(&fc);
172 		if (ret == 0) {
173 			drop_nlink(&vnode->vfs_inode);
174 			if (vnode->vfs_inode.i_nlink == 0) {
175 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
176 				clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
177 			}
178 		}
179 		if (ret == 0 &&
180 		    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
181 			afs_edit_dir_remove(dvnode, &dentry->d_name,
182 					    afs_edit_dir_for_unlink);
183 	}
184 
185 	_leave(" = %d", ret);
186 	return ret;
187 }
188 
189 /*
190  * Remove sillyrename file on iput.
191  */
192 int afs_silly_iput(struct dentry *dentry, struct inode *inode)
193 {
194 	struct afs_vnode *dvnode = AFS_FS_I(d_inode(dentry->d_parent));
195 	struct afs_vnode *vnode = AFS_FS_I(inode);
196 	struct dentry *alias;
197 	int ret;
198 
199 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
200 
201 	_enter("%p{%pd},%llx", dentry, dentry, vnode->fid.vnode);
202 
203 	down_read(&dvnode->rmdir_lock);
204 
205 	alias = d_alloc_parallel(dentry->d_parent, &dentry->d_name, &wq);
206 	if (IS_ERR(alias)) {
207 		up_read(&dvnode->rmdir_lock);
208 		return 0;
209 	}
210 
211 	if (!d_in_lookup(alias)) {
212 		/* We raced with lookup...  See if we need to transfer the
213 		 * sillyrename information to the aliased dentry.
214 		 */
215 		ret = 0;
216 		spin_lock(&alias->d_lock);
217 		if (d_really_is_positive(alias) &&
218 		    !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
219 			alias->d_flags |= DCACHE_NFSFS_RENAMED;
220 			ret = 1;
221 		}
222 		spin_unlock(&alias->d_lock);
223 		up_read(&dvnode->rmdir_lock);
224 		dput(alias);
225 		return ret;
226 	}
227 
228 	/* Stop lock-release from complaining. */
229 	spin_lock(&vnode->lock);
230 	vnode->lock_state = AFS_VNODE_LOCK_DELETED;
231 	trace_afs_flock_ev(vnode, NULL, afs_flock_silly_delete, 0);
232 	spin_unlock(&vnode->lock);
233 
234 	afs_do_silly_unlink(dvnode, vnode, dentry, dvnode->silly_key);
235 	up_read(&dvnode->rmdir_lock);
236 	d_lookup_done(alias);
237 	dput(alias);
238 	return 1;
239 }
240