unlink.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/nfs/unlink.c
  4. *
  5. * nfs sillydelete handling
  6. *
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/string.h>
  10. #include <linux/dcache.h>
  11. #include <linux/sunrpc/sched.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/nfs_fs.h>
  14. #include <linux/sched.h>
  15. #include <linux/wait.h>
  16. #include <linux/namei.h>
  17. #include <linux/fsnotify.h>
  18. #include "internal.h"
  19. #include "nfs4_fs.h"
  20. #include "iostat.h"
  21. #include "delegation.h"
  22. #include "nfstrace.h"
  23. /**
  24. * nfs_free_unlinkdata - release data from a sillydelete operation.
  25. * @data: pointer to unlink structure.
  26. */
  27. static void
  28. nfs_free_unlinkdata(struct nfs_unlinkdata *data)
  29. {
  30. put_cred(data->cred);
  31. kfree(data->args.name.name);
  32. kfree(data);
  33. }
  34. /**
  35. * nfs_async_unlink_done - Sillydelete post-processing
  36. * @task: rpc_task of the sillydelete
  37. * @calldata: pointer to nfs_unlinkdata
  38. *
  39. * Do the directory attribute update.
  40. */
  41. static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
  42. {
  43. struct nfs_unlinkdata *data = calldata;
  44. struct inode *dir = d_inode(data->dentry->d_parent);
  45. trace_nfs_sillyrename_unlink(data, task->tk_status);
  46. if (!NFS_PROTO(dir)->unlink_done(task, dir))
  47. rpc_restart_call_prepare(task);
  48. }
  49. /**
  50. * nfs_async_unlink_release - Release the sillydelete data.
  51. * @calldata: struct nfs_unlinkdata to release
  52. *
  53. * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
  54. * rpc_task would be freed too.
  55. */
  56. static void nfs_async_unlink_release(void *calldata)
  57. {
  58. struct nfs_unlinkdata *data = calldata;
  59. struct dentry *dentry = data->dentry;
  60. struct super_block *sb = dentry->d_sb;
  61. up_read_non_owner(&NFS_I(d_inode(dentry->d_parent))->rmdir_sem);
  62. d_lookup_done(dentry);
  63. nfs_free_unlinkdata(data);
  64. dput(dentry);
  65. nfs_sb_deactive(sb);
  66. }
  67. static void nfs_unlink_prepare(struct rpc_task *task, void *calldata)
  68. {
  69. struct nfs_unlinkdata *data = calldata;
  70. struct inode *dir = d_inode(data->dentry->d_parent);
  71. NFS_PROTO(dir)->unlink_rpc_prepare(task, data);
  72. }
  73. static const struct rpc_call_ops nfs_unlink_ops = {
  74. .rpc_call_done = nfs_async_unlink_done,
  75. .rpc_release = nfs_async_unlink_release,
  76. .rpc_call_prepare = nfs_unlink_prepare,
  77. };
  78. static void nfs_do_call_unlink(struct inode *inode, struct nfs_unlinkdata *data)
  79. {
  80. struct rpc_message msg = {
  81. .rpc_argp = &data->args,
  82. .rpc_resp = &data->res,
  83. .rpc_cred = data->cred,
  84. };
  85. struct rpc_task_setup task_setup_data = {
  86. .rpc_message = &msg,
  87. .callback_ops = &nfs_unlink_ops,
  88. .callback_data = data,
  89. .workqueue = nfsiod_workqueue,
  90. .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
  91. };
  92. struct rpc_task *task;
  93. struct inode *dir = d_inode(data->dentry->d_parent);
  94. nfs_sb_active(dir->i_sb);
  95. data->args.fh = NFS_FH(dir);
  96. nfs_fattr_init(data->res.dir_attr);
  97. NFS_PROTO(dir)->unlink_setup(&msg, data->dentry, inode);
  98. task_setup_data.rpc_client = NFS_CLIENT(dir);
  99. task = rpc_run_task(&task_setup_data);
  100. if (!IS_ERR(task))
  101. rpc_put_task_async(task);
  102. }
  103. static int nfs_call_unlink(struct dentry *dentry, struct inode *inode, struct nfs_unlinkdata *data)
  104. {
  105. struct inode *dir = d_inode(dentry->d_parent);
  106. struct dentry *alias;
  107. down_read_non_owner(&NFS_I(dir)->rmdir_sem);
  108. alias = d_alloc_parallel(dentry->d_parent, &data->args.name, &data->wq);
  109. if (IS_ERR(alias)) {
  110. up_read_non_owner(&NFS_I(dir)->rmdir_sem);
  111. return 0;
  112. }
  113. if (!d_in_lookup(alias)) {
  114. int ret;
  115. void *devname_garbage = NULL;
  116. /*
  117. * Hey, we raced with lookup... See if we need to transfer
  118. * the sillyrename information to the aliased dentry.
  119. */
  120. spin_lock(&alias->d_lock);
  121. if (d_really_is_positive(alias) &&
  122. !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
  123. devname_garbage = alias->d_fsdata;
  124. alias->d_fsdata = data;
  125. alias->d_flags |= DCACHE_NFSFS_RENAMED;
  126. ret = 1;
  127. } else
  128. ret = 0;
  129. spin_unlock(&alias->d_lock);
  130. dput(alias);
  131. up_read_non_owner(&NFS_I(dir)->rmdir_sem);
  132. /*
  133. * If we'd displaced old cached devname, free it. At that
  134. * point dentry is definitely not a root, so we won't need
  135. * that anymore.
  136. */
  137. kfree(devname_garbage);
  138. return ret;
  139. }
  140. data->dentry = alias;
  141. nfs_do_call_unlink(inode, data);
  142. return 1;
  143. }
  144. /**
  145. * nfs_async_unlink - asynchronous unlinking of a file
  146. * @dentry: parent directory of dentry
  147. * @name: name of dentry to unlink
  148. */
  149. static int
  150. nfs_async_unlink(struct dentry *dentry, const struct qstr *name)
  151. {
  152. struct nfs_unlinkdata *data;
  153. int status = -ENOMEM;
  154. void *devname_garbage = NULL;
  155. data = kzalloc(sizeof(*data), GFP_KERNEL);
  156. if (data == NULL)
  157. goto out;
  158. data->args.name.name = kstrdup(name->name, GFP_KERNEL);
  159. if (!data->args.name.name)
  160. goto out_free;
  161. data->args.name.len = name->len;
  162. data->cred = get_current_cred();
  163. data->res.dir_attr = &data->dir_attr;
  164. init_waitqueue_head(&data->wq);
  165. status = -EBUSY;
  166. spin_lock(&dentry->d_lock);
  167. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  168. goto out_unlock;
  169. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  170. devname_garbage = dentry->d_fsdata;
  171. dentry->d_fsdata = data;
  172. spin_unlock(&dentry->d_lock);
  173. /*
  174. * If we'd displaced old cached devname, free it. At that
  175. * point dentry is definitely not a root, so we won't need
  176. * that anymore.
  177. */
  178. kfree(devname_garbage);
  179. return 0;
  180. out_unlock:
  181. spin_unlock(&dentry->d_lock);
  182. put_cred(data->cred);
  183. kfree(data->args.name.name);
  184. out_free:
  185. kfree(data);
  186. out:
  187. return status;
  188. }
  189. /**
  190. * nfs_complete_unlink - Initialize completion of the sillydelete
  191. * @dentry: dentry to delete
  192. * @inode: inode
  193. *
  194. * Since we're most likely to be called by dentry_iput(), we
  195. * only use the dentry to find the sillydelete. We then copy the name
  196. * into the qstr.
  197. */
  198. void
  199. nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
  200. {
  201. struct nfs_unlinkdata *data;
  202. spin_lock(&dentry->d_lock);
  203. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  204. data = dentry->d_fsdata;
  205. dentry->d_fsdata = NULL;
  206. spin_unlock(&dentry->d_lock);
  207. if (NFS_STALE(inode) || !nfs_call_unlink(dentry, inode, data))
  208. nfs_free_unlinkdata(data);
  209. }
  210. /* Cancel a queued async unlink. Called when a sillyrename run fails. */
  211. static void
  212. nfs_cancel_async_unlink(struct dentry *dentry)
  213. {
  214. spin_lock(&dentry->d_lock);
  215. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  216. struct nfs_unlinkdata *data = dentry->d_fsdata;
  217. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  218. dentry->d_fsdata = NULL;
  219. spin_unlock(&dentry->d_lock);
  220. nfs_free_unlinkdata(data);
  221. return;
  222. }
  223. spin_unlock(&dentry->d_lock);
  224. }
  225. /**
  226. * nfs_async_rename_done - Sillyrename post-processing
  227. * @task: rpc_task of the sillyrename
  228. * @calldata: nfs_renamedata for the sillyrename
  229. *
  230. * Do the directory attribute updates and the d_move
  231. */
  232. static void nfs_async_rename_done(struct rpc_task *task, void *calldata)
  233. {
  234. struct nfs_renamedata *data = calldata;
  235. struct inode *old_dir = data->old_dir;
  236. struct inode *new_dir = data->new_dir;
  237. struct dentry *old_dentry = data->old_dentry;
  238. trace_nfs_sillyrename_rename(old_dir, old_dentry,
  239. new_dir, data->new_dentry, task->tk_status);
  240. if (!NFS_PROTO(old_dir)->rename_done(task, old_dir, new_dir)) {
  241. rpc_restart_call_prepare(task);
  242. return;
  243. }
  244. if (data->complete)
  245. data->complete(task, data);
  246. }
  247. /**
  248. * nfs_async_rename_release - Release the sillyrename data.
  249. * @calldata: the struct nfs_renamedata to be released
  250. */
  251. static void nfs_async_rename_release(void *calldata)
  252. {
  253. struct nfs_renamedata *data = calldata;
  254. struct super_block *sb = data->old_dir->i_sb;
  255. if (d_really_is_positive(data->old_dentry))
  256. nfs_mark_for_revalidate(d_inode(data->old_dentry));
  257. /* The result of the rename is unknown. Play it safe by
  258. * forcing a new lookup */
  259. if (data->cancelled) {
  260. spin_lock(&data->old_dir->i_lock);
  261. nfs_force_lookup_revalidate(data->old_dir);
  262. spin_unlock(&data->old_dir->i_lock);
  263. if (data->new_dir != data->old_dir) {
  264. spin_lock(&data->new_dir->i_lock);
  265. nfs_force_lookup_revalidate(data->new_dir);
  266. spin_unlock(&data->new_dir->i_lock);
  267. }
  268. }
  269. dput(data->old_dentry);
  270. dput(data->new_dentry);
  271. iput(data->old_dir);
  272. iput(data->new_dir);
  273. nfs_sb_deactive(sb);
  274. put_cred(data->cred);
  275. kfree(data);
  276. }
  277. static void nfs_rename_prepare(struct rpc_task *task, void *calldata)
  278. {
  279. struct nfs_renamedata *data = calldata;
  280. NFS_PROTO(data->old_dir)->rename_rpc_prepare(task, data);
  281. }
  282. static const struct rpc_call_ops nfs_rename_ops = {
  283. .rpc_call_done = nfs_async_rename_done,
  284. .rpc_release = nfs_async_rename_release,
  285. .rpc_call_prepare = nfs_rename_prepare,
  286. };
  287. /**
  288. * nfs_async_rename - perform an asynchronous rename operation
  289. * @old_dir: directory that currently holds the dentry to be renamed
  290. * @new_dir: target directory for the rename
  291. * @old_dentry: original dentry to be renamed
  292. * @new_dentry: dentry to which the old_dentry should be renamed
  293. * @complete: Function to run on successful completion
  294. *
  295. * It's expected that valid references to the dentries and inodes are held
  296. */
  297. struct rpc_task *
  298. nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
  299. struct dentry *old_dentry, struct dentry *new_dentry,
  300. void (*complete)(struct rpc_task *, struct nfs_renamedata *))
  301. {
  302. struct nfs_renamedata *data;
  303. struct rpc_message msg = { };
  304. struct rpc_task_setup task_setup_data = {
  305. .rpc_message = &msg,
  306. .callback_ops = &nfs_rename_ops,
  307. .workqueue = nfsiod_workqueue,
  308. .rpc_client = NFS_CLIENT(old_dir),
  309. .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
  310. };
  311. data = kzalloc(sizeof(*data), GFP_KERNEL);
  312. if (data == NULL)
  313. return ERR_PTR(-ENOMEM);
  314. task_setup_data.callback_data = data;
  315. data->cred = get_current_cred();
  316. msg.rpc_argp = &data->args;
  317. msg.rpc_resp = &data->res;
  318. msg.rpc_cred = data->cred;
  319. /* set up nfs_renamedata */
  320. data->old_dir = old_dir;
  321. ihold(old_dir);
  322. data->new_dir = new_dir;
  323. ihold(new_dir);
  324. data->old_dentry = dget(old_dentry);
  325. data->new_dentry = dget(new_dentry);
  326. nfs_fattr_init(&data->old_fattr);
  327. nfs_fattr_init(&data->new_fattr);
  328. data->complete = complete;
  329. /* set up nfs_renameargs */
  330. data->args.old_dir = NFS_FH(old_dir);
  331. data->args.old_name = &old_dentry->d_name;
  332. data->args.new_dir = NFS_FH(new_dir);
  333. data->args.new_name = &new_dentry->d_name;
  334. /* set up nfs_renameres */
  335. data->res.old_fattr = &data->old_fattr;
  336. data->res.new_fattr = &data->new_fattr;
  337. nfs_sb_active(old_dir->i_sb);
  338. NFS_PROTO(data->old_dir)->rename_setup(&msg, old_dentry, new_dentry);
  339. return rpc_run_task(&task_setup_data);
  340. }
  341. /*
  342. * Perform tasks needed when a sillyrename is done such as cancelling the
  343. * queued async unlink if it failed.
  344. */
  345. static void
  346. nfs_complete_sillyrename(struct rpc_task *task, struct nfs_renamedata *data)
  347. {
  348. struct dentry *dentry = data->old_dentry;
  349. if (task->tk_status != 0) {
  350. nfs_cancel_async_unlink(dentry);
  351. return;
  352. }
  353. }
  354. #define SILLYNAME_PREFIX ".nfs"
  355. #define SILLYNAME_PREFIX_LEN ((unsigned)sizeof(SILLYNAME_PREFIX) - 1)
  356. #define SILLYNAME_FILEID_LEN ((unsigned)sizeof(u64) << 1)
  357. #define SILLYNAME_COUNTER_LEN ((unsigned)sizeof(unsigned int) << 1)
  358. #define SILLYNAME_LEN (SILLYNAME_PREFIX_LEN + \
  359. SILLYNAME_FILEID_LEN + \
  360. SILLYNAME_COUNTER_LEN)
  361. /**
  362. * nfs_sillyrename - Perform a silly-rename of a dentry
  363. * @dir: inode of directory that contains dentry
  364. * @dentry: dentry to be sillyrenamed
  365. *
  366. * NFSv2/3 is stateless and the server doesn't know when the client is
  367. * holding a file open. To prevent application problems when a file is
  368. * unlinked while it's still open, the client performs a "silly-rename".
  369. * That is, it renames the file to a hidden file in the same directory,
  370. * and only performs the unlink once the last reference to it is put.
  371. *
  372. * The final cleanup is done during dentry_iput.
  373. *
  374. * (Note: NFSv4 is stateful, and has opens, so in theory an NFSv4 server
  375. * could take responsibility for keeping open files referenced. The server
  376. * would also need to ensure that opened-but-deleted files were kept over
  377. * reboots. However, we may not assume a server does so. (RFC 5661
  378. * does provide an OPEN4_RESULT_PRESERVE_UNLINKED flag that a server can
  379. * use to advertise that it does this; some day we may take advantage of
  380. * it.))
  381. */
  382. int
  383. nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  384. {
  385. static unsigned int sillycounter;
  386. unsigned char silly[SILLYNAME_LEN + 1];
  387. unsigned long long fileid;
  388. struct dentry *sdentry;
  389. struct inode *inode = d_inode(dentry);
  390. struct rpc_task *task;
  391. int error = -EBUSY;
  392. dfprintk(VFS, "NFS: silly-rename(%pd2, ct=%d)\n",
  393. dentry, d_count(dentry));
  394. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  395. /*
  396. * We don't allow a dentry to be silly-renamed twice.
  397. */
  398. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  399. goto out;
  400. fileid = NFS_FILEID(d_inode(dentry));
  401. sdentry = NULL;
  402. do {
  403. int slen;
  404. dput(sdentry);
  405. sillycounter++;
  406. slen = scnprintf(silly, sizeof(silly),
  407. SILLYNAME_PREFIX "%0*llx%0*x",
  408. SILLYNAME_FILEID_LEN, fileid,
  409. SILLYNAME_COUNTER_LEN, sillycounter);
  410. dfprintk(VFS, "NFS: trying to rename %pd to %s\n",
  411. dentry, silly);
  412. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  413. /*
  414. * N.B. Better to return EBUSY here ... it could be
  415. * dangerous to delete the file while it's in use.
  416. */
  417. if (IS_ERR(sdentry))
  418. goto out;
  419. } while (d_inode(sdentry) != NULL); /* need negative lookup */
  420. ihold(inode);
  421. /* queue unlink first. Can't do this from rpc_release as it
  422. * has to allocate memory
  423. */
  424. error = nfs_async_unlink(dentry, &sdentry->d_name);
  425. if (error)
  426. goto out_dput;
  427. /* run the rename task, undo unlink if it fails */
  428. task = nfs_async_rename(dir, dir, dentry, sdentry,
  429. nfs_complete_sillyrename);
  430. if (IS_ERR(task)) {
  431. error = -EBUSY;
  432. nfs_cancel_async_unlink(dentry);
  433. goto out_dput;
  434. }
  435. /* wait for the RPC task to complete, unless a SIGKILL intervenes */
  436. error = rpc_wait_for_completion_task(task);
  437. if (error == 0)
  438. error = task->tk_status;
  439. switch (error) {
  440. case 0:
  441. /* The rename succeeded */
  442. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  443. spin_lock(&inode->i_lock);
  444. NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
  445. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
  446. | NFS_INO_INVALID_CTIME
  447. | NFS_INO_REVAL_FORCED;
  448. spin_unlock(&inode->i_lock);
  449. d_move(dentry, sdentry);
  450. break;
  451. case -ERESTARTSYS:
  452. /* The result of the rename is unknown. Play it safe by
  453. * forcing a new lookup */
  454. d_drop(dentry);
  455. d_drop(sdentry);
  456. }
  457. rpc_put_task(task);
  458. out_dput:
  459. iput(inode);
  460. dput(sdentry);
  461. out:
  462. return error;
  463. }