unlink.c 14 KB

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