unlink.c 14 KB

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