unlink.c 14 KB

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