unlink.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. dput(data->old_dentry);
  261. dput(data->new_dentry);
  262. iput(data->old_dir);
  263. iput(data->new_dir);
  264. nfs_sb_deactive(sb);
  265. put_rpccred(data->cred);
  266. kfree(data);
  267. }
  268. static void nfs_rename_prepare(struct rpc_task *task, void *calldata)
  269. {
  270. struct nfs_renamedata *data = calldata;
  271. NFS_PROTO(data->old_dir)->rename_rpc_prepare(task, data);
  272. }
  273. static const struct rpc_call_ops nfs_rename_ops = {
  274. .rpc_call_done = nfs_async_rename_done,
  275. .rpc_release = nfs_async_rename_release,
  276. .rpc_call_prepare = nfs_rename_prepare,
  277. };
  278. /**
  279. * nfs_async_rename - perform an asynchronous rename operation
  280. * @old_dir: directory that currently holds the dentry to be renamed
  281. * @new_dir: target directory for the rename
  282. * @old_dentry: original dentry to be renamed
  283. * @new_dentry: dentry to which the old_dentry should be renamed
  284. *
  285. * It's expected that valid references to the dentries and inodes are held
  286. */
  287. struct rpc_task *
  288. nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
  289. struct dentry *old_dentry, struct dentry *new_dentry,
  290. void (*complete)(struct rpc_task *, struct nfs_renamedata *))
  291. {
  292. struct nfs_renamedata *data;
  293. struct rpc_message msg = { };
  294. struct rpc_task_setup task_setup_data = {
  295. .rpc_message = &msg,
  296. .callback_ops = &nfs_rename_ops,
  297. .workqueue = nfsiod_workqueue,
  298. .rpc_client = NFS_CLIENT(old_dir),
  299. .flags = RPC_TASK_ASYNC,
  300. };
  301. data = kzalloc(sizeof(*data), GFP_KERNEL);
  302. if (data == NULL)
  303. return ERR_PTR(-ENOMEM);
  304. task_setup_data.callback_data = data;
  305. data->cred = rpc_lookup_cred();
  306. if (IS_ERR(data->cred)) {
  307. struct rpc_task *task = ERR_CAST(data->cred);
  308. kfree(data);
  309. return task;
  310. }
  311. msg.rpc_argp = &data->args;
  312. msg.rpc_resp = &data->res;
  313. msg.rpc_cred = data->cred;
  314. /* set up nfs_renamedata */
  315. data->old_dir = old_dir;
  316. ihold(old_dir);
  317. data->new_dir = new_dir;
  318. ihold(new_dir);
  319. data->old_dentry = dget(old_dentry);
  320. data->new_dentry = dget(new_dentry);
  321. nfs_fattr_init(&data->old_fattr);
  322. nfs_fattr_init(&data->new_fattr);
  323. data->complete = complete;
  324. /* set up nfs_renameargs */
  325. data->args.old_dir = NFS_FH(old_dir);
  326. data->args.old_name = &old_dentry->d_name;
  327. data->args.new_dir = NFS_FH(new_dir);
  328. data->args.new_name = &new_dentry->d_name;
  329. /* set up nfs_renameres */
  330. data->res.old_fattr = &data->old_fattr;
  331. data->res.new_fattr = &data->new_fattr;
  332. nfs_sb_active(old_dir->i_sb);
  333. NFS_PROTO(data->old_dir)->rename_setup(&msg, old_dir);
  334. return rpc_run_task(&task_setup_data);
  335. }
  336. /*
  337. * Perform tasks needed when a sillyrename is done such as cancelling the
  338. * queued async unlink if it failed.
  339. */
  340. static void
  341. nfs_complete_sillyrename(struct rpc_task *task, struct nfs_renamedata *data)
  342. {
  343. struct dentry *dentry = data->old_dentry;
  344. if (task->tk_status != 0) {
  345. nfs_cancel_async_unlink(dentry);
  346. return;
  347. }
  348. /*
  349. * vfs_unlink and the like do not issue this when a file is
  350. * sillyrenamed, so do it here.
  351. */
  352. fsnotify_nameremove(dentry, 0);
  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 rpc_task *task;
  390. int error = -EBUSY;
  391. dfprintk(VFS, "NFS: silly-rename(%pd2, ct=%d)\n",
  392. dentry, d_count(dentry));
  393. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  394. /*
  395. * We don't allow a dentry to be silly-renamed twice.
  396. */
  397. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  398. goto out;
  399. fileid = NFS_FILEID(d_inode(dentry));
  400. /* Return delegation in anticipation of the rename */
  401. NFS_PROTO(d_inode(dentry))->return_delegation(d_inode(dentry));
  402. sdentry = NULL;
  403. do {
  404. int slen;
  405. dput(sdentry);
  406. sillycounter++;
  407. slen = scnprintf(silly, sizeof(silly),
  408. SILLYNAME_PREFIX "%0*llx%0*x",
  409. SILLYNAME_FILEID_LEN, fileid,
  410. SILLYNAME_COUNTER_LEN, sillycounter);
  411. dfprintk(VFS, "NFS: trying to rename %pd to %s\n",
  412. dentry, silly);
  413. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  414. /*
  415. * N.B. Better to return EBUSY here ... it could be
  416. * dangerous to delete the file while it's in use.
  417. */
  418. if (IS_ERR(sdentry))
  419. goto out;
  420. } while (d_inode(sdentry) != NULL); /* need negative lookup */
  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. d_move(dentry, sdentry);
  444. break;
  445. case -ERESTARTSYS:
  446. /* The result of the rename is unknown. Play it safe by
  447. * forcing a new lookup */
  448. d_drop(dentry);
  449. d_drop(sdentry);
  450. }
  451. rpc_put_task(task);
  452. out_dput:
  453. dput(sdentry);
  454. out:
  455. return error;
  456. }