unlink.c 15 KB

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