nfs4recover.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/slab.h>
  35. #include <linux/namei.h>
  36. #include <linux/crypto.h>
  37. #include <linux/sched.h>
  38. #include "nfsd.h"
  39. #include "state.h"
  40. #include "vfs.h"
  41. #define NFSDDBG_FACILITY NFSDDBG_PROC
  42. /* Globals */
  43. static struct file *rec_file;
  44. static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
  45. static int
  46. nfs4_save_creds(const struct cred **original_creds)
  47. {
  48. struct cred *new;
  49. new = prepare_creds();
  50. if (!new)
  51. return -ENOMEM;
  52. new->fsuid = 0;
  53. new->fsgid = 0;
  54. *original_creds = override_creds(new);
  55. put_cred(new);
  56. return 0;
  57. }
  58. static void
  59. nfs4_reset_creds(const struct cred *original)
  60. {
  61. revert_creds(original);
  62. }
  63. static void
  64. md5_to_hex(char *out, char *md5)
  65. {
  66. int i;
  67. for (i=0; i<16; i++) {
  68. unsigned char c = md5[i];
  69. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  70. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  71. }
  72. *out = '\0';
  73. }
  74. __be32
  75. nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
  76. {
  77. struct xdr_netobj cksum;
  78. struct hash_desc desc;
  79. struct scatterlist sg;
  80. __be32 status = nfserr_jukebox;
  81. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  82. clname->len, clname->data);
  83. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  84. desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  85. if (IS_ERR(desc.tfm))
  86. goto out_no_tfm;
  87. cksum.len = crypto_hash_digestsize(desc.tfm);
  88. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  89. if (cksum.data == NULL)
  90. goto out;
  91. sg_init_one(&sg, clname->data, clname->len);
  92. if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
  93. goto out;
  94. md5_to_hex(dname, cksum.data);
  95. status = nfs_ok;
  96. out:
  97. kfree(cksum.data);
  98. crypto_free_hash(desc.tfm);
  99. out_no_tfm:
  100. return status;
  101. }
  102. void nfsd4_create_clid_dir(struct nfs4_client *clp)
  103. {
  104. const struct cred *original_cred;
  105. char *dname = clp->cl_recdir;
  106. struct dentry *dir, *dentry;
  107. int status;
  108. dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
  109. if (clp->cl_firststate)
  110. return;
  111. clp->cl_firststate = 1;
  112. if (!rec_file)
  113. return;
  114. status = nfs4_save_creds(&original_cred);
  115. if (status < 0)
  116. return;
  117. dir = rec_file->f_path.dentry;
  118. /* lock the parent */
  119. mutex_lock(&dir->d_inode->i_mutex);
  120. dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
  121. if (IS_ERR(dentry)) {
  122. status = PTR_ERR(dentry);
  123. goto out_unlock;
  124. }
  125. if (dentry->d_inode)
  126. /*
  127. * In the 4.1 case, where we're called from
  128. * reclaim_complete(), records from the previous reboot
  129. * may still be left, so this is OK.
  130. *
  131. * In the 4.0 case, we should never get here; but we may
  132. * as well be forgiving and just succeed silently.
  133. */
  134. goto out_put;
  135. status = mnt_want_write_file(rec_file);
  136. if (status)
  137. goto out_put;
  138. status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
  139. mnt_drop_write_file(rec_file);
  140. out_put:
  141. dput(dentry);
  142. out_unlock:
  143. mutex_unlock(&dir->d_inode->i_mutex);
  144. if (status == 0)
  145. vfs_fsync(rec_file, 0);
  146. else
  147. printk(KERN_ERR "NFSD: failed to write recovery record"
  148. " (err %d); please check that %s exists"
  149. " and is writeable", status,
  150. user_recovery_dirname);
  151. nfs4_reset_creds(original_cred);
  152. }
  153. typedef int (recdir_func)(struct dentry *, struct dentry *);
  154. struct name_list {
  155. char name[HEXDIR_LEN];
  156. struct list_head list;
  157. };
  158. static int
  159. nfsd4_build_namelist(void *arg, const char *name, int namlen,
  160. loff_t offset, u64 ino, unsigned int d_type)
  161. {
  162. struct list_head *names = arg;
  163. struct name_list *entry;
  164. if (namlen != HEXDIR_LEN - 1)
  165. return 0;
  166. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  167. if (entry == NULL)
  168. return -ENOMEM;
  169. memcpy(entry->name, name, HEXDIR_LEN - 1);
  170. entry->name[HEXDIR_LEN - 1] = '\0';
  171. list_add(&entry->list, names);
  172. return 0;
  173. }
  174. static int
  175. nfsd4_list_rec_dir(recdir_func *f)
  176. {
  177. const struct cred *original_cred;
  178. struct dentry *dir = rec_file->f_path.dentry;
  179. LIST_HEAD(names);
  180. int status;
  181. status = nfs4_save_creds(&original_cred);
  182. if (status < 0)
  183. return status;
  184. status = vfs_llseek(rec_file, 0, SEEK_SET);
  185. if (status < 0) {
  186. nfs4_reset_creds(original_cred);
  187. return status;
  188. }
  189. status = vfs_readdir(rec_file, nfsd4_build_namelist, &names);
  190. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  191. while (!list_empty(&names)) {
  192. struct name_list *entry;
  193. entry = list_entry(names.next, struct name_list, list);
  194. if (!status) {
  195. struct dentry *dentry;
  196. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  197. if (IS_ERR(dentry)) {
  198. status = PTR_ERR(dentry);
  199. break;
  200. }
  201. status = f(dir, dentry);
  202. dput(dentry);
  203. }
  204. list_del(&entry->list);
  205. kfree(entry);
  206. }
  207. mutex_unlock(&dir->d_inode->i_mutex);
  208. nfs4_reset_creds(original_cred);
  209. return status;
  210. }
  211. static int
  212. nfsd4_unlink_clid_dir(char *name, int namlen)
  213. {
  214. struct dentry *dir, *dentry;
  215. int status;
  216. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  217. dir = rec_file->f_path.dentry;
  218. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  219. dentry = lookup_one_len(name, dir, namlen);
  220. if (IS_ERR(dentry)) {
  221. status = PTR_ERR(dentry);
  222. goto out_unlock;
  223. }
  224. status = -ENOENT;
  225. if (!dentry->d_inode)
  226. goto out;
  227. status = vfs_rmdir(dir->d_inode, dentry);
  228. out:
  229. dput(dentry);
  230. out_unlock:
  231. mutex_unlock(&dir->d_inode->i_mutex);
  232. return status;
  233. }
  234. void
  235. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  236. {
  237. const struct cred *original_cred;
  238. int status;
  239. if (!rec_file || !clp->cl_firststate)
  240. return;
  241. status = mnt_want_write_file(rec_file);
  242. if (status)
  243. goto out;
  244. clp->cl_firststate = 0;
  245. status = nfs4_save_creds(&original_cred);
  246. if (status < 0)
  247. goto out;
  248. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  249. nfs4_reset_creds(original_cred);
  250. if (status == 0)
  251. vfs_fsync(rec_file, 0);
  252. mnt_drop_write_file(rec_file);
  253. out:
  254. if (status)
  255. printk("NFSD: Failed to remove expired client state directory"
  256. " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
  257. return;
  258. }
  259. static int
  260. purge_old(struct dentry *parent, struct dentry *child)
  261. {
  262. int status;
  263. if (nfs4_has_reclaimed_state(child->d_name.name, false))
  264. return 0;
  265. status = vfs_rmdir(parent->d_inode, child);
  266. if (status)
  267. printk("failed to remove client recovery directory %s\n",
  268. child->d_name.name);
  269. /* Keep trying, success or failure: */
  270. return 0;
  271. }
  272. void
  273. nfsd4_recdir_purge_old(void) {
  274. int status;
  275. if (!rec_file)
  276. return;
  277. status = mnt_want_write_file(rec_file);
  278. if (status)
  279. goto out;
  280. status = nfsd4_list_rec_dir(purge_old);
  281. if (status == 0)
  282. vfs_fsync(rec_file, 0);
  283. mnt_drop_write_file(rec_file);
  284. out:
  285. if (status)
  286. printk("nfsd4: failed to purge old clients from recovery"
  287. " directory %s\n", rec_file->f_path.dentry->d_name.name);
  288. }
  289. static int
  290. load_recdir(struct dentry *parent, struct dentry *child)
  291. {
  292. if (child->d_name.len != HEXDIR_LEN - 1) {
  293. printk("nfsd4: illegal name %s in recovery directory\n",
  294. child->d_name.name);
  295. /* Keep trying; maybe the others are OK: */
  296. return 0;
  297. }
  298. nfs4_client_to_reclaim(child->d_name.name);
  299. return 0;
  300. }
  301. int
  302. nfsd4_recdir_load(void) {
  303. int status;
  304. if (!rec_file)
  305. return 0;
  306. status = nfsd4_list_rec_dir(load_recdir);
  307. if (status)
  308. printk("nfsd4: failed loading clients from recovery"
  309. " directory %s\n", rec_file->f_path.dentry->d_name.name);
  310. return status;
  311. }
  312. /*
  313. * Hold reference to the recovery directory.
  314. */
  315. void
  316. nfsd4_init_recdir()
  317. {
  318. const struct cred *original_cred;
  319. int status;
  320. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  321. user_recovery_dirname);
  322. BUG_ON(rec_file);
  323. status = nfs4_save_creds(&original_cred);
  324. if (status < 0) {
  325. printk("NFSD: Unable to change credentials to find recovery"
  326. " directory: error %d\n",
  327. status);
  328. return;
  329. }
  330. rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
  331. if (IS_ERR(rec_file)) {
  332. printk("NFSD: unable to find recovery directory %s\n",
  333. user_recovery_dirname);
  334. rec_file = NULL;
  335. }
  336. nfs4_reset_creds(original_cred);
  337. }
  338. void
  339. nfsd4_shutdown_recdir(void)
  340. {
  341. if (!rec_file)
  342. return;
  343. fput(rec_file);
  344. rec_file = NULL;
  345. }
  346. /*
  347. * Change the NFSv4 recovery directory to recdir.
  348. */
  349. int
  350. nfs4_reset_recoverydir(char *recdir)
  351. {
  352. int status;
  353. struct path path;
  354. status = kern_path(recdir, LOOKUP_FOLLOW, &path);
  355. if (status)
  356. return status;
  357. status = -ENOTDIR;
  358. if (S_ISDIR(path.dentry->d_inode->i_mode)) {
  359. strcpy(user_recovery_dirname, recdir);
  360. status = 0;
  361. }
  362. path_put(&path);
  363. return status;
  364. }
  365. char *
  366. nfs4_recoverydir(void)
  367. {
  368. return user_recovery_dirname;
  369. }