nfs4recover.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2004 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Andy Adamson <andros@citi.umich.edu>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the University nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  27. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <linux/file.h>
  34. #include <linux/namei.h>
  35. #include <linux/crypto.h>
  36. #include <linux/sched.h>
  37. #include "nfsd.h"
  38. #include "state.h"
  39. #include "vfs.h"
  40. #define NFSDDBG_FACILITY NFSDDBG_PROC
  41. /* Globals */
  42. static struct path rec_dir;
  43. static int rec_dir_init = 0;
  44. static int
  45. nfs4_save_creds(const struct cred **original_creds)
  46. {
  47. struct cred *new;
  48. new = prepare_creds();
  49. if (!new)
  50. return -ENOMEM;
  51. new->fsuid = 0;
  52. new->fsgid = 0;
  53. *original_creds = override_creds(new);
  54. put_cred(new);
  55. return 0;
  56. }
  57. static void
  58. nfs4_reset_creds(const struct cred *original)
  59. {
  60. revert_creds(original);
  61. }
  62. static void
  63. md5_to_hex(char *out, char *md5)
  64. {
  65. int i;
  66. for (i=0; i<16; i++) {
  67. unsigned char c = md5[i];
  68. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  69. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  70. }
  71. *out = '\0';
  72. }
  73. __be32
  74. nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
  75. {
  76. struct xdr_netobj cksum;
  77. struct hash_desc desc;
  78. struct scatterlist sg;
  79. __be32 status = nfserr_resource;
  80. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  81. clname->len, clname->data);
  82. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  83. desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  84. if (IS_ERR(desc.tfm))
  85. goto out_no_tfm;
  86. cksum.len = crypto_hash_digestsize(desc.tfm);
  87. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  88. if (cksum.data == NULL)
  89. goto out;
  90. sg_init_one(&sg, clname->data, clname->len);
  91. if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
  92. goto out;
  93. md5_to_hex(dname, cksum.data);
  94. status = nfs_ok;
  95. out:
  96. kfree(cksum.data);
  97. crypto_free_hash(desc.tfm);
  98. out_no_tfm:
  99. return status;
  100. }
  101. static void
  102. nfsd4_sync_rec_dir(void)
  103. {
  104. mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
  105. nfsd_sync_dir(rec_dir.dentry);
  106. mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
  107. }
  108. int
  109. nfsd4_create_clid_dir(struct nfs4_client *clp)
  110. {
  111. const struct cred *original_cred;
  112. char *dname = clp->cl_recdir;
  113. struct dentry *dentry;
  114. int status;
  115. dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
  116. if (!rec_dir_init || clp->cl_firststate)
  117. return 0;
  118. status = nfs4_save_creds(&original_cred);
  119. if (status < 0)
  120. return status;
  121. /* lock the parent */
  122. mutex_lock(&rec_dir.dentry->d_inode->i_mutex);
  123. dentry = lookup_one_len(dname, rec_dir.dentry, HEXDIR_LEN-1);
  124. if (IS_ERR(dentry)) {
  125. status = PTR_ERR(dentry);
  126. goto out_unlock;
  127. }
  128. status = -EEXIST;
  129. if (dentry->d_inode) {
  130. dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
  131. goto out_put;
  132. }
  133. status = mnt_want_write(rec_dir.mnt);
  134. if (status)
  135. goto out_put;
  136. status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
  137. mnt_drop_write(rec_dir.mnt);
  138. out_put:
  139. dput(dentry);
  140. out_unlock:
  141. mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
  142. if (status == 0) {
  143. clp->cl_firststate = 1;
  144. nfsd4_sync_rec_dir();
  145. }
  146. nfs4_reset_creds(original_cred);
  147. dprintk("NFSD: nfsd4_create_clid_dir returns %d\n", status);
  148. return status;
  149. }
  150. typedef int (recdir_func)(struct dentry *, struct dentry *);
  151. struct name_list {
  152. char name[HEXDIR_LEN];
  153. struct list_head list;
  154. };
  155. static int
  156. nfsd4_build_namelist(void *arg, const char *name, int namlen,
  157. loff_t offset, u64 ino, unsigned int d_type)
  158. {
  159. struct list_head *names = arg;
  160. struct name_list *entry;
  161. if (namlen != HEXDIR_LEN - 1)
  162. return 0;
  163. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  164. if (entry == NULL)
  165. return -ENOMEM;
  166. memcpy(entry->name, name, HEXDIR_LEN - 1);
  167. entry->name[HEXDIR_LEN - 1] = '\0';
  168. list_add(&entry->list, names);
  169. return 0;
  170. }
  171. static int
  172. nfsd4_list_rec_dir(struct dentry *dir, recdir_func *f)
  173. {
  174. const struct cred *original_cred;
  175. struct file *filp;
  176. LIST_HEAD(names);
  177. struct name_list *entry;
  178. struct dentry *dentry;
  179. int status;
  180. if (!rec_dir_init)
  181. return 0;
  182. status = nfs4_save_creds(&original_cred);
  183. if (status < 0)
  184. return status;
  185. filp = dentry_open(dget(dir), mntget(rec_dir.mnt), O_RDONLY,
  186. current_cred());
  187. status = PTR_ERR(filp);
  188. if (IS_ERR(filp))
  189. goto out;
  190. status = vfs_readdir(filp, nfsd4_build_namelist, &names);
  191. fput(filp);
  192. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  193. while (!list_empty(&names)) {
  194. entry = list_entry(names.next, struct name_list, list);
  195. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  196. if (IS_ERR(dentry)) {
  197. status = PTR_ERR(dentry);
  198. break;
  199. }
  200. status = f(dir, dentry);
  201. dput(dentry);
  202. if (status)
  203. break;
  204. list_del(&entry->list);
  205. kfree(entry);
  206. }
  207. mutex_unlock(&dir->d_inode->i_mutex);
  208. out:
  209. while (!list_empty(&names)) {
  210. entry = list_entry(names.next, struct name_list, list);
  211. list_del(&entry->list);
  212. kfree(entry);
  213. }
  214. nfs4_reset_creds(original_cred);
  215. return status;
  216. }
  217. static int
  218. nfsd4_unlink_clid_dir(char *name, int namlen)
  219. {
  220. struct dentry *dentry;
  221. int status;
  222. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  223. mutex_lock_nested(&rec_dir.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  224. dentry = lookup_one_len(name, rec_dir.dentry, namlen);
  225. if (IS_ERR(dentry)) {
  226. status = PTR_ERR(dentry);
  227. goto out_unlock;
  228. }
  229. status = -ENOENT;
  230. if (!dentry->d_inode)
  231. goto out;
  232. status = vfs_rmdir(rec_dir.dentry->d_inode, dentry);
  233. out:
  234. dput(dentry);
  235. out_unlock:
  236. mutex_unlock(&rec_dir.dentry->d_inode->i_mutex);
  237. return status;
  238. }
  239. void
  240. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  241. {
  242. const struct cred *original_cred;
  243. int status;
  244. if (!rec_dir_init || !clp->cl_firststate)
  245. return;
  246. status = mnt_want_write(rec_dir.mnt);
  247. if (status)
  248. goto out;
  249. clp->cl_firststate = 0;
  250. status = nfs4_save_creds(&original_cred);
  251. if (status < 0)
  252. goto out;
  253. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  254. nfs4_reset_creds(original_cred);
  255. if (status == 0)
  256. nfsd4_sync_rec_dir();
  257. mnt_drop_write(rec_dir.mnt);
  258. out:
  259. if (status)
  260. printk("NFSD: Failed to remove expired client state directory"
  261. " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
  262. return;
  263. }
  264. static int
  265. purge_old(struct dentry *parent, struct dentry *child)
  266. {
  267. int status;
  268. /* note: we currently use this path only for minorversion 0 */
  269. if (nfs4_has_reclaimed_state(child->d_name.name, false))
  270. return 0;
  271. status = vfs_rmdir(parent->d_inode, child);
  272. if (status)
  273. printk("failed to remove client recovery directory %s\n",
  274. child->d_name.name);
  275. /* Keep trying, success or failure: */
  276. return 0;
  277. }
  278. void
  279. nfsd4_recdir_purge_old(void) {
  280. int status;
  281. if (!rec_dir_init)
  282. return;
  283. status = mnt_want_write(rec_dir.mnt);
  284. if (status)
  285. goto out;
  286. status = nfsd4_list_rec_dir(rec_dir.dentry, purge_old);
  287. if (status == 0)
  288. nfsd4_sync_rec_dir();
  289. mnt_drop_write(rec_dir.mnt);
  290. out:
  291. if (status)
  292. printk("nfsd4: failed to purge old clients from recovery"
  293. " directory %s\n", rec_dir.dentry->d_name.name);
  294. }
  295. static int
  296. load_recdir(struct dentry *parent, struct dentry *child)
  297. {
  298. if (child->d_name.len != HEXDIR_LEN - 1) {
  299. printk("nfsd4: illegal name %s in recovery directory\n",
  300. child->d_name.name);
  301. /* Keep trying; maybe the others are OK: */
  302. return 0;
  303. }
  304. nfs4_client_to_reclaim(child->d_name.name);
  305. return 0;
  306. }
  307. int
  308. nfsd4_recdir_load(void) {
  309. int status;
  310. status = nfsd4_list_rec_dir(rec_dir.dentry, load_recdir);
  311. if (status)
  312. printk("nfsd4: failed loading clients from recovery"
  313. " directory %s\n", rec_dir.dentry->d_name.name);
  314. return status;
  315. }
  316. /*
  317. * Hold reference to the recovery directory.
  318. */
  319. void
  320. nfsd4_init_recdir(char *rec_dirname)
  321. {
  322. const struct cred *original_cred;
  323. int status;
  324. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  325. rec_dirname);
  326. BUG_ON(rec_dir_init);
  327. status = nfs4_save_creds(&original_cred);
  328. if (status < 0) {
  329. printk("NFSD: Unable to change credentials to find recovery"
  330. " directory: error %d\n",
  331. status);
  332. return;
  333. }
  334. status = kern_path(rec_dirname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
  335. &rec_dir);
  336. if (status)
  337. printk("NFSD: unable to find recovery directory %s\n",
  338. rec_dirname);
  339. if (!status)
  340. rec_dir_init = 1;
  341. nfs4_reset_creds(original_cred);
  342. }
  343. void
  344. nfsd4_shutdown_recdir(void)
  345. {
  346. if (!rec_dir_init)
  347. return;
  348. rec_dir_init = 0;
  349. path_put(&rec_dir);
  350. }