unlink.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. kfree(devname_garbage);
  243. return 0;
  244. out_unlock:
  245. spin_unlock(&dentry->d_lock);
  246. put_rpccred(data->cred);
  247. out_free:
  248. kfree(data);
  249. out:
  250. return status;
  251. }
  252. /**
  253. * nfs_complete_unlink - Initialize completion of the sillydelete
  254. * @dentry: dentry to delete
  255. * @inode: inode
  256. *
  257. * Since we're most likely to be called by dentry_iput(), we
  258. * only use the dentry to find the sillydelete. We then copy the name
  259. * into the qstr.
  260. */
  261. void
  262. nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
  263. {
  264. struct nfs_unlinkdata *data = NULL;
  265. spin_lock(&dentry->d_lock);
  266. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  267. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  268. data = dentry->d_fsdata;
  269. dentry->d_fsdata = NULL;
  270. }
  271. spin_unlock(&dentry->d_lock);
  272. if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
  273. nfs_free_unlinkdata(data);
  274. }
  275. /* Cancel a queued async unlink. Called when a sillyrename run fails. */
  276. static void
  277. nfs_cancel_async_unlink(struct dentry *dentry)
  278. {
  279. spin_lock(&dentry->d_lock);
  280. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  281. struct nfs_unlinkdata *data = dentry->d_fsdata;
  282. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  283. dentry->d_fsdata = NULL;
  284. spin_unlock(&dentry->d_lock);
  285. nfs_free_unlinkdata(data);
  286. return;
  287. }
  288. spin_unlock(&dentry->d_lock);
  289. }
  290. /**
  291. * nfs_async_rename_done - Sillyrename post-processing
  292. * @task: rpc_task of the sillyrename
  293. * @calldata: nfs_renamedata for the sillyrename
  294. *
  295. * Do the directory attribute updates and the d_move
  296. */
  297. static void nfs_async_rename_done(struct rpc_task *task, void *calldata)
  298. {
  299. struct nfs_renamedata *data = calldata;
  300. struct inode *old_dir = data->old_dir;
  301. struct inode *new_dir = data->new_dir;
  302. struct dentry *old_dentry = data->old_dentry;
  303. if (!NFS_PROTO(old_dir)->rename_done(task, old_dir, new_dir)) {
  304. rpc_restart_call_prepare(task);
  305. return;
  306. }
  307. if (task->tk_status != 0)
  308. nfs_cancel_async_unlink(old_dentry);
  309. }
  310. /**
  311. * nfs_async_rename_release - Release the sillyrename data.
  312. * @calldata: the struct nfs_renamedata to be released
  313. */
  314. static void nfs_async_rename_release(void *calldata)
  315. {
  316. struct nfs_renamedata *data = calldata;
  317. struct super_block *sb = data->old_dir->i_sb;
  318. if (data->old_dentry->d_inode)
  319. nfs_mark_for_revalidate(data->old_dentry->d_inode);
  320. dput(data->old_dentry);
  321. dput(data->new_dentry);
  322. iput(data->old_dir);
  323. iput(data->new_dir);
  324. nfs_sb_deactive(sb);
  325. put_rpccred(data->cred);
  326. kfree(data);
  327. }
  328. static void nfs_rename_prepare(struct rpc_task *task, void *calldata)
  329. {
  330. struct nfs_renamedata *data = calldata;
  331. NFS_PROTO(data->old_dir)->rename_rpc_prepare(task, data);
  332. }
  333. static const struct rpc_call_ops nfs_rename_ops = {
  334. .rpc_call_done = nfs_async_rename_done,
  335. .rpc_release = nfs_async_rename_release,
  336. .rpc_call_prepare = nfs_rename_prepare,
  337. };
  338. /**
  339. * nfs_async_rename - perform an asynchronous rename operation
  340. * @old_dir: directory that currently holds the dentry to be renamed
  341. * @new_dir: target directory for the rename
  342. * @old_dentry: original dentry to be renamed
  343. * @new_dentry: dentry to which the old_dentry should be renamed
  344. *
  345. * It's expected that valid references to the dentries and inodes are held
  346. */
  347. static struct rpc_task *
  348. nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
  349. struct dentry *old_dentry, struct dentry *new_dentry)
  350. {
  351. struct nfs_renamedata *data;
  352. struct rpc_message msg = { };
  353. struct rpc_task_setup task_setup_data = {
  354. .rpc_message = &msg,
  355. .callback_ops = &nfs_rename_ops,
  356. .workqueue = nfsiod_workqueue,
  357. .rpc_client = NFS_CLIENT(old_dir),
  358. .flags = RPC_TASK_ASYNC,
  359. };
  360. data = kzalloc(sizeof(*data), GFP_KERNEL);
  361. if (data == NULL)
  362. return ERR_PTR(-ENOMEM);
  363. task_setup_data.callback_data = data;
  364. data->cred = rpc_lookup_cred();
  365. if (IS_ERR(data->cred)) {
  366. struct rpc_task *task = ERR_CAST(data->cred);
  367. kfree(data);
  368. return task;
  369. }
  370. msg.rpc_argp = &data->args;
  371. msg.rpc_resp = &data->res;
  372. msg.rpc_cred = data->cred;
  373. /* set up nfs_renamedata */
  374. data->old_dir = old_dir;
  375. ihold(old_dir);
  376. data->new_dir = new_dir;
  377. ihold(new_dir);
  378. data->old_dentry = dget(old_dentry);
  379. data->new_dentry = dget(new_dentry);
  380. nfs_fattr_init(&data->old_fattr);
  381. nfs_fattr_init(&data->new_fattr);
  382. /* set up nfs_renameargs */
  383. data->args.old_dir = NFS_FH(old_dir);
  384. data->args.old_name = &old_dentry->d_name;
  385. data->args.new_dir = NFS_FH(new_dir);
  386. data->args.new_name = &new_dentry->d_name;
  387. /* set up nfs_renameres */
  388. data->res.old_fattr = &data->old_fattr;
  389. data->res.new_fattr = &data->new_fattr;
  390. nfs_sb_active(old_dir->i_sb);
  391. NFS_PROTO(data->old_dir)->rename_setup(&msg, old_dir);
  392. return rpc_run_task(&task_setup_data);
  393. }
  394. /**
  395. * nfs_sillyrename - Perform a silly-rename of a dentry
  396. * @dir: inode of directory that contains dentry
  397. * @dentry: dentry to be sillyrenamed
  398. *
  399. * NFSv2/3 is stateless and the server doesn't know when the client is
  400. * holding a file open. To prevent application problems when a file is
  401. * unlinked while it's still open, the client performs a "silly-rename".
  402. * That is, it renames the file to a hidden file in the same directory,
  403. * and only performs the unlink once the last reference to it is put.
  404. *
  405. * The final cleanup is done during dentry_iput.
  406. *
  407. * (Note: NFSv4 is stateful, and has opens, so in theory an NFSv4 server
  408. * could take responsibility for keeping open files referenced. The server
  409. * would also need to ensure that opened-but-deleted files were kept over
  410. * reboots. However, we may not assume a server does so. (RFC 5661
  411. * does provide an OPEN4_RESULT_PRESERVE_UNLINKED flag that a server can
  412. * use to advertise that it does this; some day we may take advantage of
  413. * it.))
  414. */
  415. int
  416. nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  417. {
  418. static unsigned int sillycounter;
  419. const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
  420. const int countersize = sizeof(sillycounter)*2;
  421. const int slen = sizeof(".nfs")+fileidsize+countersize-1;
  422. char silly[slen+1];
  423. struct dentry *sdentry;
  424. struct rpc_task *task;
  425. int error = -EIO;
  426. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  427. dentry->d_parent->d_name.name, dentry->d_name.name,
  428. dentry->d_count);
  429. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  430. /*
  431. * We don't allow a dentry to be silly-renamed twice.
  432. */
  433. error = -EBUSY;
  434. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  435. goto out;
  436. sprintf(silly, ".nfs%*.*Lx",
  437. fileidsize, fileidsize,
  438. (unsigned long long)NFS_FILEID(dentry->d_inode));
  439. /* Return delegation in anticipation of the rename */
  440. NFS_PROTO(dentry->d_inode)->return_delegation(dentry->d_inode);
  441. sdentry = NULL;
  442. do {
  443. char *suffix = silly + slen - countersize;
  444. dput(sdentry);
  445. sillycounter++;
  446. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  447. dfprintk(VFS, "NFS: trying to rename %s to %s\n",
  448. dentry->d_name.name, silly);
  449. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  450. /*
  451. * N.B. Better to return EBUSY here ... it could be
  452. * dangerous to delete the file while it's in use.
  453. */
  454. if (IS_ERR(sdentry))
  455. goto out;
  456. } while (sdentry->d_inode != NULL); /* need negative lookup */
  457. /* queue unlink first. Can't do this from rpc_release as it
  458. * has to allocate memory
  459. */
  460. error = nfs_async_unlink(dir, dentry);
  461. if (error)
  462. goto out_dput;
  463. /* populate unlinkdata with the right dname */
  464. error = nfs_copy_dname(sdentry,
  465. (struct nfs_unlinkdata *)dentry->d_fsdata);
  466. if (error) {
  467. nfs_cancel_async_unlink(dentry);
  468. goto out_dput;
  469. }
  470. /* run the rename task, undo unlink if it fails */
  471. task = nfs_async_rename(dir, dir, dentry, sdentry);
  472. if (IS_ERR(task)) {
  473. error = -EBUSY;
  474. nfs_cancel_async_unlink(dentry);
  475. goto out_dput;
  476. }
  477. /* wait for the RPC task to complete, unless a SIGKILL intervenes */
  478. error = rpc_wait_for_completion_task(task);
  479. if (error == 0)
  480. error = task->tk_status;
  481. switch (error) {
  482. case 0:
  483. /* The rename succeeded */
  484. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  485. d_move(dentry, sdentry);
  486. break;
  487. case -ERESTARTSYS:
  488. /* The result of the rename is unknown. Play it safe by
  489. * forcing a new lookup */
  490. d_drop(dentry);
  491. d_drop(sdentry);
  492. }
  493. rpc_put_task(task);
  494. out_dput:
  495. dput(sdentry);
  496. out:
  497. return error;
  498. }