unlink.c 14 KB

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