unlink.c 15 KB

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