unlink.c 14 KB

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