unlink.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 = 0;
  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. spin_lock(&alias->d_lock);
  141. if (alias->d_inode != NULL &&
  142. !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
  143. devname_garbage = alias->d_fsdata;
  144. alias->d_fsdata = data;
  145. alias->d_flags |= DCACHE_NFSFS_RENAMED;
  146. ret = 1;
  147. }
  148. spin_unlock(&alias->d_lock);
  149. nfs_dec_sillycount(dir);
  150. dput(alias);
  151. /*
  152. * If we'd displaced old cached devname, free it. At that
  153. * point dentry is definitely not a root, so we won't need
  154. * that anymore.
  155. */
  156. if (devname_garbage)
  157. kfree(devname_garbage);
  158. return ret;
  159. }
  160. data->dir = igrab(dir);
  161. if (!data->dir) {
  162. nfs_dec_sillycount(dir);
  163. return 0;
  164. }
  165. nfs_sb_active(dir->i_sb);
  166. data->args.fh = NFS_FH(dir);
  167. nfs_fattr_init(data->res.dir_attr);
  168. NFS_PROTO(dir)->unlink_setup(&msg, dir);
  169. task_setup_data.rpc_client = NFS_CLIENT(dir);
  170. task = rpc_run_task(&task_setup_data);
  171. if (!IS_ERR(task))
  172. rpc_put_task_async(task);
  173. return 1;
  174. }
  175. static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
  176. {
  177. struct dentry *parent;
  178. struct inode *dir;
  179. int ret = 0;
  180. parent = dget_parent(dentry);
  181. if (parent == NULL)
  182. goto out_free;
  183. dir = parent->d_inode;
  184. if (nfs_copy_dname(dentry, data) != 0)
  185. goto out_dput;
  186. /* Non-exclusive lock protects against concurrent lookup() calls */
  187. spin_lock(&dir->i_lock);
  188. if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
  189. /* Deferred delete */
  190. hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
  191. spin_unlock(&dir->i_lock);
  192. ret = 1;
  193. goto out_dput;
  194. }
  195. spin_unlock(&dir->i_lock);
  196. ret = nfs_do_call_unlink(parent, dir, data);
  197. out_dput:
  198. dput(parent);
  199. out_free:
  200. return ret;
  201. }
  202. void nfs_block_sillyrename(struct dentry *dentry)
  203. {
  204. struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
  205. wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
  206. }
  207. void nfs_unblock_sillyrename(struct dentry *dentry)
  208. {
  209. struct inode *dir = dentry->d_inode;
  210. struct nfs_inode *nfsi = NFS_I(dir);
  211. struct nfs_unlinkdata *data;
  212. atomic_inc(&nfsi->silly_count);
  213. spin_lock(&dir->i_lock);
  214. while (!hlist_empty(&nfsi->silly_list)) {
  215. if (!atomic_inc_not_zero(&nfsi->silly_count))
  216. break;
  217. data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
  218. hlist_del(&data->list);
  219. spin_unlock(&dir->i_lock);
  220. if (nfs_do_call_unlink(dentry, dir, data) == 0)
  221. nfs_free_unlinkdata(data);
  222. spin_lock(&dir->i_lock);
  223. }
  224. spin_unlock(&dir->i_lock);
  225. }
  226. /**
  227. * nfs_async_unlink - asynchronous unlinking of a file
  228. * @dir: parent directory of dentry
  229. * @dentry: dentry to unlink
  230. */
  231. static int
  232. nfs_async_unlink(struct inode *dir, struct dentry *dentry)
  233. {
  234. struct nfs_unlinkdata *data;
  235. int status = -ENOMEM;
  236. void *devname_garbage = NULL;
  237. data = kzalloc(sizeof(*data), GFP_KERNEL);
  238. if (data == NULL)
  239. goto out;
  240. data->cred = rpc_lookup_cred();
  241. if (IS_ERR(data->cred)) {
  242. status = PTR_ERR(data->cred);
  243. goto out_free;
  244. }
  245. data->res.dir_attr = &data->dir_attr;
  246. status = -EBUSY;
  247. spin_lock(&dentry->d_lock);
  248. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  249. goto out_unlock;
  250. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  251. devname_garbage = dentry->d_fsdata;
  252. dentry->d_fsdata = data;
  253. spin_unlock(&dentry->d_lock);
  254. /*
  255. * If we'd displaced old cached devname, free it. At that
  256. * point dentry is definitely not a root, so we won't need
  257. * that anymore.
  258. */
  259. if (devname_garbage)
  260. kfree(devname_garbage);
  261. return 0;
  262. out_unlock:
  263. spin_unlock(&dentry->d_lock);
  264. put_rpccred(data->cred);
  265. out_free:
  266. kfree(data);
  267. out:
  268. return status;
  269. }
  270. /**
  271. * nfs_complete_unlink - Initialize completion of the sillydelete
  272. * @dentry: dentry to delete
  273. * @inode: inode
  274. *
  275. * Since we're most likely to be called by dentry_iput(), we
  276. * only use the dentry to find the sillydelete. We then copy the name
  277. * into the qstr.
  278. */
  279. void
  280. nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
  281. {
  282. struct nfs_unlinkdata *data = NULL;
  283. spin_lock(&dentry->d_lock);
  284. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  285. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  286. data = dentry->d_fsdata;
  287. dentry->d_fsdata = NULL;
  288. }
  289. spin_unlock(&dentry->d_lock);
  290. if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
  291. nfs_free_unlinkdata(data);
  292. }
  293. /* Cancel a queued async unlink. Called when a sillyrename run fails. */
  294. static void
  295. nfs_cancel_async_unlink(struct dentry *dentry)
  296. {
  297. spin_lock(&dentry->d_lock);
  298. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  299. struct nfs_unlinkdata *data = dentry->d_fsdata;
  300. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  301. dentry->d_fsdata = NULL;
  302. spin_unlock(&dentry->d_lock);
  303. nfs_free_unlinkdata(data);
  304. return;
  305. }
  306. spin_unlock(&dentry->d_lock);
  307. }
  308. struct nfs_renamedata {
  309. struct nfs_renameargs args;
  310. struct nfs_renameres res;
  311. struct rpc_cred *cred;
  312. struct inode *old_dir;
  313. struct dentry *old_dentry;
  314. struct nfs_fattr old_fattr;
  315. struct inode *new_dir;
  316. struct dentry *new_dentry;
  317. struct nfs_fattr new_fattr;
  318. };
  319. /**
  320. * nfs_async_rename_done - Sillyrename post-processing
  321. * @task: rpc_task of the sillyrename
  322. * @calldata: nfs_renamedata for the sillyrename
  323. *
  324. * Do the directory attribute updates and the d_move
  325. */
  326. static void nfs_async_rename_done(struct rpc_task *task, void *calldata)
  327. {
  328. struct nfs_renamedata *data = calldata;
  329. struct inode *old_dir = data->old_dir;
  330. struct inode *new_dir = data->new_dir;
  331. if (!NFS_PROTO(old_dir)->rename_done(task, old_dir, new_dir)) {
  332. nfs_restart_rpc(task, NFS_SERVER(old_dir)->nfs_client);
  333. return;
  334. }
  335. if (task->tk_status != 0) {
  336. nfs_cancel_async_unlink(data->old_dentry);
  337. return;
  338. }
  339. nfs_set_verifier(data->old_dentry, nfs_save_change_attribute(old_dir));
  340. d_move(data->old_dentry, data->new_dentry);
  341. }
  342. /**
  343. * nfs_async_rename_release - Release the sillyrename data.
  344. * @calldata: the struct nfs_renamedata to be released
  345. */
  346. static void nfs_async_rename_release(void *calldata)
  347. {
  348. struct nfs_renamedata *data = calldata;
  349. struct super_block *sb = data->old_dir->i_sb;
  350. if (data->old_dentry->d_inode)
  351. nfs_mark_for_revalidate(data->old_dentry->d_inode);
  352. dput(data->old_dentry);
  353. dput(data->new_dentry);
  354. iput(data->old_dir);
  355. iput(data->new_dir);
  356. nfs_sb_deactive(sb);
  357. put_rpccred(data->cred);
  358. kfree(data);
  359. }
  360. #if defined(CONFIG_NFS_V4_1)
  361. static void nfs_rename_prepare(struct rpc_task *task, void *calldata)
  362. {
  363. struct nfs_renamedata *data = calldata;
  364. struct nfs_server *server = NFS_SERVER(data->old_dir);
  365. if (nfs4_setup_sequence(server, &data->args.seq_args,
  366. &data->res.seq_res, 1, task))
  367. return;
  368. rpc_call_start(task);
  369. }
  370. #endif /* CONFIG_NFS_V4_1 */
  371. static const struct rpc_call_ops nfs_rename_ops = {
  372. .rpc_call_done = nfs_async_rename_done,
  373. .rpc_release = nfs_async_rename_release,
  374. #if defined(CONFIG_NFS_V4_1)
  375. .rpc_call_prepare = nfs_rename_prepare,
  376. #endif /* CONFIG_NFS_V4_1 */
  377. };
  378. /**
  379. * nfs_async_rename - perform an asynchronous rename operation
  380. * @old_dir: directory that currently holds the dentry to be renamed
  381. * @new_dir: target directory for the rename
  382. * @old_dentry: original dentry to be renamed
  383. * @new_dentry: dentry to which the old_dentry should be renamed
  384. *
  385. * It's expected that valid references to the dentries and inodes are held
  386. */
  387. static struct rpc_task *
  388. nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
  389. struct dentry *old_dentry, struct dentry *new_dentry)
  390. {
  391. struct nfs_renamedata *data;
  392. struct rpc_message msg = { };
  393. struct rpc_task_setup task_setup_data = {
  394. .rpc_message = &msg,
  395. .callback_ops = &nfs_rename_ops,
  396. .workqueue = nfsiod_workqueue,
  397. .rpc_client = NFS_CLIENT(old_dir),
  398. .flags = RPC_TASK_ASYNC,
  399. };
  400. data = kzalloc(sizeof(*data), GFP_KERNEL);
  401. if (data == NULL)
  402. return ERR_PTR(-ENOMEM);
  403. task_setup_data.callback_data = data;
  404. data->cred = rpc_lookup_cred();
  405. if (IS_ERR(data->cred)) {
  406. struct rpc_task *task = ERR_CAST(data->cred);
  407. kfree(data);
  408. return task;
  409. }
  410. msg.rpc_argp = &data->args;
  411. msg.rpc_resp = &data->res;
  412. msg.rpc_cred = data->cred;
  413. /* set up nfs_renamedata */
  414. data->old_dir = old_dir;
  415. ihold(old_dir);
  416. data->new_dir = new_dir;
  417. ihold(new_dir);
  418. data->old_dentry = dget(old_dentry);
  419. data->new_dentry = dget(new_dentry);
  420. nfs_fattr_init(&data->old_fattr);
  421. nfs_fattr_init(&data->new_fattr);
  422. /* set up nfs_renameargs */
  423. data->args.old_dir = NFS_FH(old_dir);
  424. data->args.old_name = &old_dentry->d_name;
  425. data->args.new_dir = NFS_FH(new_dir);
  426. data->args.new_name = &new_dentry->d_name;
  427. /* set up nfs_renameres */
  428. data->res.old_fattr = &data->old_fattr;
  429. data->res.new_fattr = &data->new_fattr;
  430. nfs_sb_active(old_dir->i_sb);
  431. NFS_PROTO(data->old_dir)->rename_setup(&msg, old_dir);
  432. return rpc_run_task(&task_setup_data);
  433. }
  434. /**
  435. * nfs_sillyrename - Perform a silly-rename of a dentry
  436. * @dir: inode of directory that contains dentry
  437. * @dentry: dentry to be sillyrenamed
  438. *
  439. * NFSv2/3 is stateless and the server doesn't know when the client is
  440. * holding a file open. To prevent application problems when a file is
  441. * unlinked while it's still open, the client performs a "silly-rename".
  442. * That is, it renames the file to a hidden file in the same directory,
  443. * and only performs the unlink once the last reference to it is put.
  444. *
  445. * The final cleanup is done during dentry_iput.
  446. */
  447. int
  448. nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  449. {
  450. static unsigned int sillycounter;
  451. const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
  452. const int countersize = sizeof(sillycounter)*2;
  453. const int slen = sizeof(".nfs")+fileidsize+countersize-1;
  454. char silly[slen+1];
  455. struct dentry *sdentry;
  456. struct rpc_task *task;
  457. int error = -EIO;
  458. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  459. dentry->d_parent->d_name.name, dentry->d_name.name,
  460. dentry->d_count);
  461. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  462. /*
  463. * We don't allow a dentry to be silly-renamed twice.
  464. */
  465. error = -EBUSY;
  466. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  467. goto out;
  468. sprintf(silly, ".nfs%*.*Lx",
  469. fileidsize, fileidsize,
  470. (unsigned long long)NFS_FILEID(dentry->d_inode));
  471. /* Return delegation in anticipation of the rename */
  472. nfs_inode_return_delegation(dentry->d_inode);
  473. sdentry = NULL;
  474. do {
  475. char *suffix = silly + slen - countersize;
  476. dput(sdentry);
  477. sillycounter++;
  478. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  479. dfprintk(VFS, "NFS: trying to rename %s to %s\n",
  480. dentry->d_name.name, silly);
  481. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  482. /*
  483. * N.B. Better to return EBUSY here ... it could be
  484. * dangerous to delete the file while it's in use.
  485. */
  486. if (IS_ERR(sdentry))
  487. goto out;
  488. } while (sdentry->d_inode != NULL); /* need negative lookup */
  489. /* queue unlink first. Can't do this from rpc_release as it
  490. * has to allocate memory
  491. */
  492. error = nfs_async_unlink(dir, dentry);
  493. if (error)
  494. goto out_dput;
  495. /* run the rename task, undo unlink if it fails */
  496. task = nfs_async_rename(dir, dir, dentry, sdentry);
  497. if (IS_ERR(task)) {
  498. error = -EBUSY;
  499. nfs_cancel_async_unlink(dentry);
  500. goto out_dput;
  501. }
  502. /* wait for the RPC task to complete, unless a SIGKILL intervenes */
  503. error = rpc_wait_for_completion_task(task);
  504. if (error == 0)
  505. error = task->tk_status;
  506. rpc_put_task(task);
  507. out_dput:
  508. dput(sdentry);
  509. out:
  510. return error;
  511. }