unlink.c 15 KB

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