unlink.c 14 KB

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