nfsctl.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /*
  2. * Syscall interface to knfsd.
  3. *
  4. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/namei.h>
  8. #include <linux/ctype.h>
  9. #include <linux/sunrpc/svcsock.h>
  10. #include <linux/lockd/lockd.h>
  11. #include <linux/sunrpc/addr.h>
  12. #include <linux/sunrpc/gss_api.h>
  13. #include <linux/sunrpc/gss_krb5_enctypes.h>
  14. #include <linux/sunrpc/rpc_pipe_fs.h>
  15. #include <linux/module.h>
  16. #include "idmap.h"
  17. #include "nfsd.h"
  18. #include "cache.h"
  19. #include "state.h"
  20. #include "netns.h"
  21. /*
  22. * We have a single directory with several nodes in it.
  23. */
  24. enum {
  25. NFSD_Root = 1,
  26. NFSD_List,
  27. NFSD_Export_features,
  28. NFSD_Fh,
  29. NFSD_FO_UnlockIP,
  30. NFSD_FO_UnlockFS,
  31. NFSD_Threads,
  32. NFSD_Pool_Threads,
  33. NFSD_Pool_Stats,
  34. NFSD_Versions,
  35. NFSD_Ports,
  36. NFSD_MaxBlkSize,
  37. NFSD_SupportedEnctypes,
  38. /*
  39. * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
  40. * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
  41. */
  42. #ifdef CONFIG_NFSD_V4
  43. NFSD_Leasetime,
  44. NFSD_Gracetime,
  45. NFSD_RecoveryDir,
  46. #endif
  47. };
  48. /*
  49. * write() for these nodes.
  50. */
  51. static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
  52. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
  53. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
  54. static ssize_t write_threads(struct file *file, char *buf, size_t size);
  55. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
  56. static ssize_t write_versions(struct file *file, char *buf, size_t size);
  57. static ssize_t write_ports(struct file *file, char *buf, size_t size);
  58. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
  59. #ifdef CONFIG_NFSD_V4
  60. static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
  61. static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
  62. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
  63. #endif
  64. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  65. [NFSD_Fh] = write_filehandle,
  66. [NFSD_FO_UnlockIP] = write_unlock_ip,
  67. [NFSD_FO_UnlockFS] = write_unlock_fs,
  68. [NFSD_Threads] = write_threads,
  69. [NFSD_Pool_Threads] = write_pool_threads,
  70. [NFSD_Versions] = write_versions,
  71. [NFSD_Ports] = write_ports,
  72. [NFSD_MaxBlkSize] = write_maxblksize,
  73. #ifdef CONFIG_NFSD_V4
  74. [NFSD_Leasetime] = write_leasetime,
  75. [NFSD_Gracetime] = write_gracetime,
  76. [NFSD_RecoveryDir] = write_recoverydir,
  77. #endif
  78. };
  79. static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  80. {
  81. ino_t ino = file_inode(file)->i_ino;
  82. char *data;
  83. ssize_t rv;
  84. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  85. return -EINVAL;
  86. data = simple_transaction_get(file, buf, size);
  87. if (IS_ERR(data))
  88. return PTR_ERR(data);
  89. rv = write_op[ino](file, data, size);
  90. if (rv >= 0) {
  91. simple_transaction_set(file, rv);
  92. rv = size;
  93. }
  94. return rv;
  95. }
  96. static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  97. {
  98. if (! file->private_data) {
  99. /* An attempt to read a transaction file without writing
  100. * causes a 0-byte write so that the file can return
  101. * state information
  102. */
  103. ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
  104. if (rv < 0)
  105. return rv;
  106. }
  107. return simple_transaction_read(file, buf, size, pos);
  108. }
  109. static const struct file_operations transaction_ops = {
  110. .write = nfsctl_transaction_write,
  111. .read = nfsctl_transaction_read,
  112. .release = simple_transaction_release,
  113. .llseek = default_llseek,
  114. };
  115. static int exports_net_open(struct net *net, struct file *file)
  116. {
  117. int err;
  118. struct seq_file *seq;
  119. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  120. err = seq_open(file, &nfs_exports_op);
  121. if (err)
  122. return err;
  123. seq = file->private_data;
  124. seq->private = nn->svc_export_cache;
  125. return 0;
  126. }
  127. static int exports_proc_open(struct inode *inode, struct file *file)
  128. {
  129. return exports_net_open(current->nsproxy->net_ns, file);
  130. }
  131. static const struct file_operations exports_proc_operations = {
  132. .open = exports_proc_open,
  133. .read = seq_read,
  134. .llseek = seq_lseek,
  135. .release = seq_release,
  136. .owner = THIS_MODULE,
  137. };
  138. static int exports_nfsd_open(struct inode *inode, struct file *file)
  139. {
  140. return exports_net_open(inode->i_sb->s_fs_info, file);
  141. }
  142. static const struct file_operations exports_nfsd_operations = {
  143. .open = exports_nfsd_open,
  144. .read = seq_read,
  145. .llseek = seq_lseek,
  146. .release = seq_release,
  147. .owner = THIS_MODULE,
  148. };
  149. static int export_features_show(struct seq_file *m, void *v)
  150. {
  151. seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
  152. return 0;
  153. }
  154. static int export_features_open(struct inode *inode, struct file *file)
  155. {
  156. return single_open(file, export_features_show, NULL);
  157. }
  158. static struct file_operations export_features_operations = {
  159. .open = export_features_open,
  160. .read = seq_read,
  161. .llseek = seq_lseek,
  162. .release = single_release,
  163. };
  164. #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
  165. static int supported_enctypes_show(struct seq_file *m, void *v)
  166. {
  167. seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
  168. return 0;
  169. }
  170. static int supported_enctypes_open(struct inode *inode, struct file *file)
  171. {
  172. return single_open(file, supported_enctypes_show, NULL);
  173. }
  174. static struct file_operations supported_enctypes_ops = {
  175. .open = supported_enctypes_open,
  176. .read = seq_read,
  177. .llseek = seq_lseek,
  178. .release = single_release,
  179. };
  180. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  181. static const struct file_operations pool_stats_operations = {
  182. .open = nfsd_pool_stats_open,
  183. .read = seq_read,
  184. .llseek = seq_lseek,
  185. .release = nfsd_pool_stats_release,
  186. .owner = THIS_MODULE,
  187. };
  188. /*----------------------------------------------------------------------------*/
  189. /*
  190. * payload - write methods
  191. */
  192. /**
  193. * write_unlock_ip - Release all locks used by a client
  194. *
  195. * Experimental.
  196. *
  197. * Input:
  198. * buf: '\n'-terminated C string containing a
  199. * presentation format IP address
  200. * size: length of C string in @buf
  201. * Output:
  202. * On success: returns zero if all specified locks were released;
  203. * returns one if one or more locks were not released
  204. * On error: return code is negative errno value
  205. */
  206. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
  207. {
  208. struct sockaddr_storage address;
  209. struct sockaddr *sap = (struct sockaddr *)&address;
  210. size_t salen = sizeof(address);
  211. char *fo_path;
  212. struct net *net = file->f_dentry->d_sb->s_fs_info;
  213. /* sanity check */
  214. if (size == 0)
  215. return -EINVAL;
  216. if (buf[size-1] != '\n')
  217. return -EINVAL;
  218. fo_path = buf;
  219. if (qword_get(&buf, fo_path, size) < 0)
  220. return -EINVAL;
  221. if (rpc_pton(net, fo_path, size, sap, salen) == 0)
  222. return -EINVAL;
  223. return nlmsvc_unlock_all_by_ip(sap);
  224. }
  225. /**
  226. * write_unlock_fs - Release all locks on a local file system
  227. *
  228. * Experimental.
  229. *
  230. * Input:
  231. * buf: '\n'-terminated C string containing the
  232. * absolute pathname of a local file system
  233. * size: length of C string in @buf
  234. * Output:
  235. * On success: returns zero if all specified locks were released;
  236. * returns one if one or more locks were not released
  237. * On error: return code is negative errno value
  238. */
  239. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
  240. {
  241. struct path path;
  242. char *fo_path;
  243. int error;
  244. /* sanity check */
  245. if (size == 0)
  246. return -EINVAL;
  247. if (buf[size-1] != '\n')
  248. return -EINVAL;
  249. fo_path = buf;
  250. if (qword_get(&buf, fo_path, size) < 0)
  251. return -EINVAL;
  252. error = kern_path(fo_path, 0, &path);
  253. if (error)
  254. return error;
  255. /*
  256. * XXX: Needs better sanity checking. Otherwise we could end up
  257. * releasing locks on the wrong file system.
  258. *
  259. * For example:
  260. * 1. Does the path refer to a directory?
  261. * 2. Is that directory a mount point, or
  262. * 3. Is that directory the root of an exported file system?
  263. */
  264. error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
  265. path_put(&path);
  266. return error;
  267. }
  268. /**
  269. * write_filehandle - Get a variable-length NFS file handle by path
  270. *
  271. * On input, the buffer contains a '\n'-terminated C string comprised of
  272. * three alphanumeric words separated by whitespace. The string may
  273. * contain escape sequences.
  274. *
  275. * Input:
  276. * buf:
  277. * domain: client domain name
  278. * path: export pathname
  279. * maxsize: numeric maximum size of
  280. * @buf
  281. * size: length of C string in @buf
  282. * Output:
  283. * On success: passed-in buffer filled with '\n'-terminated C
  284. * string containing a ASCII hex text version
  285. * of the NFS file handle;
  286. * return code is the size in bytes of the string
  287. * On error: return code is negative errno value
  288. */
  289. static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
  290. {
  291. char *dname, *path;
  292. int uninitialized_var(maxsize);
  293. char *mesg = buf;
  294. int len;
  295. struct auth_domain *dom;
  296. struct knfsd_fh fh;
  297. struct net *net = file->f_dentry->d_sb->s_fs_info;
  298. if (size == 0)
  299. return -EINVAL;
  300. if (buf[size-1] != '\n')
  301. return -EINVAL;
  302. buf[size-1] = 0;
  303. dname = mesg;
  304. len = qword_get(&mesg, dname, size);
  305. if (len <= 0)
  306. return -EINVAL;
  307. path = dname+len+1;
  308. len = qword_get(&mesg, path, size);
  309. if (len <= 0)
  310. return -EINVAL;
  311. len = get_int(&mesg, &maxsize);
  312. if (len)
  313. return len;
  314. if (maxsize < NFS_FHSIZE)
  315. return -EINVAL;
  316. if (maxsize > NFS3_FHSIZE)
  317. maxsize = NFS3_FHSIZE;
  318. if (qword_get(&mesg, mesg, size)>0)
  319. return -EINVAL;
  320. /* we have all the words, they are in buf.. */
  321. dom = unix_domain_find(dname);
  322. if (!dom)
  323. return -ENOMEM;
  324. len = exp_rootfh(net, dom, path, &fh, maxsize);
  325. auth_domain_put(dom);
  326. if (len)
  327. return len;
  328. mesg = buf;
  329. len = SIMPLE_TRANSACTION_LIMIT;
  330. qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
  331. mesg[-1] = '\n';
  332. return mesg - buf;
  333. }
  334. /**
  335. * write_threads - Start NFSD, or report the current number of running threads
  336. *
  337. * Input:
  338. * buf: ignored
  339. * size: zero
  340. * Output:
  341. * On success: passed-in buffer filled with '\n'-terminated C
  342. * string numeric value representing the number of
  343. * running NFSD threads;
  344. * return code is the size in bytes of the string
  345. * On error: return code is zero
  346. *
  347. * OR
  348. *
  349. * Input:
  350. * buf: C string containing an unsigned
  351. * integer value representing the
  352. * number of NFSD threads to start
  353. * size: non-zero length of C string in @buf
  354. * Output:
  355. * On success: NFS service is started;
  356. * passed-in buffer filled with '\n'-terminated C
  357. * string numeric value representing the number of
  358. * running NFSD threads;
  359. * return code is the size in bytes of the string
  360. * On error: return code is zero or a negative errno value
  361. */
  362. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  363. {
  364. char *mesg = buf;
  365. int rv;
  366. struct net *net = file->f_dentry->d_sb->s_fs_info;
  367. if (size > 0) {
  368. int newthreads;
  369. rv = get_int(&mesg, &newthreads);
  370. if (rv)
  371. return rv;
  372. if (newthreads < 0)
  373. return -EINVAL;
  374. rv = nfsd_svc(newthreads, net);
  375. if (rv < 0)
  376. return rv;
  377. } else
  378. rv = nfsd_nrthreads(net);
  379. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
  380. }
  381. /**
  382. * write_pool_threads - Set or report the current number of threads per pool
  383. *
  384. * Input:
  385. * buf: ignored
  386. * size: zero
  387. *
  388. * OR
  389. *
  390. * Input:
  391. * buf: C string containing whitespace-
  392. * separated unsigned integer values
  393. * representing the number of NFSD
  394. * threads to start in each pool
  395. * size: non-zero length of C string in @buf
  396. * Output:
  397. * On success: passed-in buffer filled with '\n'-terminated C
  398. * string containing integer values representing the
  399. * number of NFSD threads in each pool;
  400. * return code is the size in bytes of the string
  401. * On error: return code is zero or a negative errno value
  402. */
  403. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
  404. {
  405. /* if size > 0, look for an array of number of threads per node
  406. * and apply them then write out number of threads per node as reply
  407. */
  408. char *mesg = buf;
  409. int i;
  410. int rv;
  411. int len;
  412. int npools;
  413. int *nthreads;
  414. struct net *net = file->f_dentry->d_sb->s_fs_info;
  415. mutex_lock(&nfsd_mutex);
  416. npools = nfsd_nrpools(net);
  417. if (npools == 0) {
  418. /*
  419. * NFS is shut down. The admin can start it by
  420. * writing to the threads file but NOT the pool_threads
  421. * file, sorry. Report zero threads.
  422. */
  423. mutex_unlock(&nfsd_mutex);
  424. strcpy(buf, "0\n");
  425. return strlen(buf);
  426. }
  427. nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
  428. rv = -ENOMEM;
  429. if (nthreads == NULL)
  430. goto out_free;
  431. if (size > 0) {
  432. for (i = 0; i < npools; i++) {
  433. rv = get_int(&mesg, &nthreads[i]);
  434. if (rv == -ENOENT)
  435. break; /* fewer numbers than pools */
  436. if (rv)
  437. goto out_free; /* syntax error */
  438. rv = -EINVAL;
  439. if (nthreads[i] < 0)
  440. goto out_free;
  441. }
  442. rv = nfsd_set_nrthreads(i, nthreads, net);
  443. if (rv)
  444. goto out_free;
  445. }
  446. rv = nfsd_get_nrthreads(npools, nthreads, net);
  447. if (rv)
  448. goto out_free;
  449. mesg = buf;
  450. size = SIMPLE_TRANSACTION_LIMIT;
  451. for (i = 0; i < npools && size > 0; i++) {
  452. snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
  453. len = strlen(mesg);
  454. size -= len;
  455. mesg += len;
  456. }
  457. rv = mesg - buf;
  458. out_free:
  459. kfree(nthreads);
  460. mutex_unlock(&nfsd_mutex);
  461. return rv;
  462. }
  463. static ssize_t __write_versions(struct file *file, char *buf, size_t size)
  464. {
  465. char *mesg = buf;
  466. char *vers, *minorp, sign;
  467. int len, num, remaining;
  468. unsigned minor;
  469. ssize_t tlen = 0;
  470. char *sep;
  471. struct net *net = file->f_dentry->d_sb->s_fs_info;
  472. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  473. if (size>0) {
  474. if (nn->nfsd_serv)
  475. /* Cannot change versions without updating
  476. * nn->nfsd_serv->sv_xdrsize, and reallocing
  477. * rq_argp and rq_resp
  478. */
  479. return -EBUSY;
  480. if (buf[size-1] != '\n')
  481. return -EINVAL;
  482. buf[size-1] = 0;
  483. vers = mesg;
  484. len = qword_get(&mesg, vers, size);
  485. if (len <= 0) return -EINVAL;
  486. do {
  487. sign = *vers;
  488. if (sign == '+' || sign == '-')
  489. num = simple_strtol((vers+1), &minorp, 0);
  490. else
  491. num = simple_strtol(vers, &minorp, 0);
  492. if (*minorp == '.') {
  493. if (num != 4)
  494. return -EINVAL;
  495. minor = simple_strtoul(minorp+1, NULL, 0);
  496. if (minor == 0)
  497. return -EINVAL;
  498. if (nfsd_minorversion(minor, sign == '-' ?
  499. NFSD_CLEAR : NFSD_SET) < 0)
  500. return -EINVAL;
  501. goto next;
  502. }
  503. switch(num) {
  504. case 2:
  505. case 3:
  506. case 4:
  507. nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
  508. break;
  509. default:
  510. return -EINVAL;
  511. }
  512. next:
  513. vers += len + 1;
  514. } while ((len = qword_get(&mesg, vers, size)) > 0);
  515. /* If all get turned off, turn them back on, as
  516. * having no versions is BAD
  517. */
  518. nfsd_reset_versions();
  519. }
  520. /* Now write current state into reply buffer */
  521. len = 0;
  522. sep = "";
  523. remaining = SIMPLE_TRANSACTION_LIMIT;
  524. for (num=2 ; num <= 4 ; num++)
  525. if (nfsd_vers(num, NFSD_AVAIL)) {
  526. len = snprintf(buf, remaining, "%s%c%d", sep,
  527. nfsd_vers(num, NFSD_TEST)?'+':'-',
  528. num);
  529. sep = " ";
  530. if (len > remaining)
  531. break;
  532. remaining -= len;
  533. buf += len;
  534. tlen += len;
  535. }
  536. if (nfsd_vers(4, NFSD_AVAIL))
  537. for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
  538. minor++) {
  539. len = snprintf(buf, remaining, " %c4.%u",
  540. (nfsd_vers(4, NFSD_TEST) &&
  541. nfsd_minorversion(minor, NFSD_TEST)) ?
  542. '+' : '-',
  543. minor);
  544. if (len > remaining)
  545. break;
  546. remaining -= len;
  547. buf += len;
  548. tlen += len;
  549. }
  550. len = snprintf(buf, remaining, "\n");
  551. if (len > remaining)
  552. return -EINVAL;
  553. return tlen + len;
  554. }
  555. /**
  556. * write_versions - Set or report the available NFS protocol versions
  557. *
  558. * Input:
  559. * buf: ignored
  560. * size: zero
  561. * Output:
  562. * On success: passed-in buffer filled with '\n'-terminated C
  563. * string containing positive or negative integer
  564. * values representing the current status of each
  565. * protocol version;
  566. * return code is the size in bytes of the string
  567. * On error: return code is zero or a negative errno value
  568. *
  569. * OR
  570. *
  571. * Input:
  572. * buf: C string containing whitespace-
  573. * separated positive or negative
  574. * integer values representing NFS
  575. * protocol versions to enable ("+n")
  576. * or disable ("-n")
  577. * size: non-zero length of C string in @buf
  578. * Output:
  579. * On success: status of zero or more protocol versions has
  580. * been updated; passed-in buffer filled with
  581. * '\n'-terminated C string containing positive
  582. * or negative integer values representing the
  583. * current status of each protocol version;
  584. * return code is the size in bytes of the string
  585. * On error: return code is zero or a negative errno value
  586. */
  587. static ssize_t write_versions(struct file *file, char *buf, size_t size)
  588. {
  589. ssize_t rv;
  590. mutex_lock(&nfsd_mutex);
  591. rv = __write_versions(file, buf, size);
  592. mutex_unlock(&nfsd_mutex);
  593. return rv;
  594. }
  595. /*
  596. * Zero-length write. Return a list of NFSD's current listener
  597. * transports.
  598. */
  599. static ssize_t __write_ports_names(char *buf, struct net *net)
  600. {
  601. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  602. if (nn->nfsd_serv == NULL)
  603. return 0;
  604. return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
  605. }
  606. /*
  607. * A single 'fd' number was written, in which case it must be for
  608. * a socket of a supported family/protocol, and we use it as an
  609. * nfsd listener.
  610. */
  611. static ssize_t __write_ports_addfd(char *buf, struct net *net)
  612. {
  613. char *mesg = buf;
  614. int fd, err;
  615. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  616. err = get_int(&mesg, &fd);
  617. if (err != 0 || fd < 0)
  618. return -EINVAL;
  619. err = nfsd_create_serv(net);
  620. if (err != 0)
  621. return err;
  622. err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
  623. if (err < 0) {
  624. nfsd_destroy(net);
  625. return err;
  626. }
  627. /* Decrease the count, but don't shut down the service */
  628. nn->nfsd_serv->sv_nrthreads--;
  629. return err;
  630. }
  631. /*
  632. * A transport listener is added by writing it's transport name and
  633. * a port number.
  634. */
  635. static ssize_t __write_ports_addxprt(char *buf, struct net *net)
  636. {
  637. char transport[16];
  638. struct svc_xprt *xprt;
  639. int port, err;
  640. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  641. if (sscanf(buf, "%15s %5u", transport, &port) != 2)
  642. return -EINVAL;
  643. if (port < 1 || port > USHRT_MAX)
  644. return -EINVAL;
  645. err = nfsd_create_serv(net);
  646. if (err != 0)
  647. return err;
  648. err = svc_create_xprt(nn->nfsd_serv, transport, net,
  649. PF_INET, port, SVC_SOCK_ANONYMOUS);
  650. if (err < 0)
  651. goto out_err;
  652. err = svc_create_xprt(nn->nfsd_serv, transport, net,
  653. PF_INET6, port, SVC_SOCK_ANONYMOUS);
  654. if (err < 0 && err != -EAFNOSUPPORT)
  655. goto out_close;
  656. /* Decrease the count, but don't shut down the service */
  657. nn->nfsd_serv->sv_nrthreads--;
  658. return 0;
  659. out_close:
  660. xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
  661. if (xprt != NULL) {
  662. svc_close_xprt(xprt);
  663. svc_xprt_put(xprt);
  664. }
  665. out_err:
  666. nfsd_destroy(net);
  667. return err;
  668. }
  669. static ssize_t __write_ports(struct file *file, char *buf, size_t size,
  670. struct net *net)
  671. {
  672. if (size == 0)
  673. return __write_ports_names(buf, net);
  674. if (isdigit(buf[0]))
  675. return __write_ports_addfd(buf, net);
  676. if (isalpha(buf[0]))
  677. return __write_ports_addxprt(buf, net);
  678. return -EINVAL;
  679. }
  680. /**
  681. * write_ports - Pass a socket file descriptor or transport name to listen on
  682. *
  683. * Input:
  684. * buf: ignored
  685. * size: zero
  686. * Output:
  687. * On success: passed-in buffer filled with a '\n'-terminated C
  688. * string containing a whitespace-separated list of
  689. * named NFSD listeners;
  690. * return code is the size in bytes of the string
  691. * On error: return code is zero or a negative errno value
  692. *
  693. * OR
  694. *
  695. * Input:
  696. * buf: C string containing an unsigned
  697. * integer value representing a bound
  698. * but unconnected socket that is to be
  699. * used as an NFSD listener; listen(3)
  700. * must be called for a SOCK_STREAM
  701. * socket, otherwise it is ignored
  702. * size: non-zero length of C string in @buf
  703. * Output:
  704. * On success: NFS service is started;
  705. * passed-in buffer filled with a '\n'-terminated C
  706. * string containing a unique alphanumeric name of
  707. * the listener;
  708. * return code is the size in bytes of the string
  709. * On error: return code is a negative errno value
  710. *
  711. * OR
  712. *
  713. * Input:
  714. * buf: C string containing a transport
  715. * name and an unsigned integer value
  716. * representing the port to listen on,
  717. * separated by whitespace
  718. * size: non-zero length of C string in @buf
  719. * Output:
  720. * On success: returns zero; NFS service is started
  721. * On error: return code is a negative errno value
  722. */
  723. static ssize_t write_ports(struct file *file, char *buf, size_t size)
  724. {
  725. ssize_t rv;
  726. struct net *net = file->f_dentry->d_sb->s_fs_info;
  727. mutex_lock(&nfsd_mutex);
  728. rv = __write_ports(file, buf, size, net);
  729. mutex_unlock(&nfsd_mutex);
  730. return rv;
  731. }
  732. int nfsd_max_blksize;
  733. /**
  734. * write_maxblksize - Set or report the current NFS blksize
  735. *
  736. * Input:
  737. * buf: ignored
  738. * size: zero
  739. *
  740. * OR
  741. *
  742. * Input:
  743. * buf: C string containing an unsigned
  744. * integer value representing the new
  745. * NFS blksize
  746. * size: non-zero length of C string in @buf
  747. * Output:
  748. * On success: passed-in buffer filled with '\n'-terminated C string
  749. * containing numeric value of the current NFS blksize
  750. * setting;
  751. * return code is the size in bytes of the string
  752. * On error: return code is zero or a negative errno value
  753. */
  754. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
  755. {
  756. char *mesg = buf;
  757. struct net *net = file->f_dentry->d_sb->s_fs_info;
  758. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  759. if (size > 0) {
  760. int bsize;
  761. int rv = get_int(&mesg, &bsize);
  762. if (rv)
  763. return rv;
  764. /* force bsize into allowed range and
  765. * required alignment.
  766. */
  767. if (bsize < 1024)
  768. bsize = 1024;
  769. if (bsize > NFSSVC_MAXBLKSIZE)
  770. bsize = NFSSVC_MAXBLKSIZE;
  771. bsize &= ~(1024-1);
  772. mutex_lock(&nfsd_mutex);
  773. if (nn->nfsd_serv) {
  774. mutex_unlock(&nfsd_mutex);
  775. return -EBUSY;
  776. }
  777. nfsd_max_blksize = bsize;
  778. mutex_unlock(&nfsd_mutex);
  779. }
  780. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
  781. nfsd_max_blksize);
  782. }
  783. #ifdef CONFIG_NFSD_V4
  784. static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
  785. time_t *time, struct nfsd_net *nn)
  786. {
  787. char *mesg = buf;
  788. int rv, i;
  789. if (size > 0) {
  790. if (nn->nfsd_serv)
  791. return -EBUSY;
  792. rv = get_int(&mesg, &i);
  793. if (rv)
  794. return rv;
  795. /*
  796. * Some sanity checking. We don't have a reason for
  797. * these particular numbers, but problems with the
  798. * extremes are:
  799. * - Too short: the briefest network outage may
  800. * cause clients to lose all their locks. Also,
  801. * the frequent polling may be wasteful.
  802. * - Too long: do you really want reboot recovery
  803. * to take more than an hour? Or to make other
  804. * clients wait an hour before being able to
  805. * revoke a dead client's locks?
  806. */
  807. if (i < 10 || i > 3600)
  808. return -EINVAL;
  809. *time = i;
  810. }
  811. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
  812. }
  813. static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
  814. time_t *time, struct nfsd_net *nn)
  815. {
  816. ssize_t rv;
  817. mutex_lock(&nfsd_mutex);
  818. rv = __nfsd4_write_time(file, buf, size, time, nn);
  819. mutex_unlock(&nfsd_mutex);
  820. return rv;
  821. }
  822. /**
  823. * write_leasetime - Set or report the current NFSv4 lease time
  824. *
  825. * Input:
  826. * buf: ignored
  827. * size: zero
  828. *
  829. * OR
  830. *
  831. * Input:
  832. * buf: C string containing an unsigned
  833. * integer value representing the new
  834. * NFSv4 lease expiry time
  835. * size: non-zero length of C string in @buf
  836. * Output:
  837. * On success: passed-in buffer filled with '\n'-terminated C
  838. * string containing unsigned integer value of the
  839. * current lease expiry time;
  840. * return code is the size in bytes of the string
  841. * On error: return code is zero or a negative errno value
  842. */
  843. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  844. {
  845. struct net *net = file->f_dentry->d_sb->s_fs_info;
  846. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  847. return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
  848. }
  849. /**
  850. * write_gracetime - Set or report current NFSv4 grace period time
  851. *
  852. * As above, but sets the time of the NFSv4 grace period.
  853. *
  854. * Note this should never be set to less than the *previous*
  855. * lease-period time, but we don't try to enforce this. (In the common
  856. * case (a new boot), we don't know what the previous lease time was
  857. * anyway.)
  858. */
  859. static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
  860. {
  861. struct net *net = file->f_dentry->d_sb->s_fs_info;
  862. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  863. return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
  864. }
  865. static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
  866. struct nfsd_net *nn)
  867. {
  868. char *mesg = buf;
  869. char *recdir;
  870. int len, status;
  871. if (size > 0) {
  872. if (nn->nfsd_serv)
  873. return -EBUSY;
  874. if (size > PATH_MAX || buf[size-1] != '\n')
  875. return -EINVAL;
  876. buf[size-1] = 0;
  877. recdir = mesg;
  878. len = qword_get(&mesg, recdir, size);
  879. if (len <= 0)
  880. return -EINVAL;
  881. status = nfs4_reset_recoverydir(recdir);
  882. if (status)
  883. return status;
  884. }
  885. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
  886. nfs4_recoverydir());
  887. }
  888. /**
  889. * write_recoverydir - Set or report the pathname of the recovery directory
  890. *
  891. * Input:
  892. * buf: ignored
  893. * size: zero
  894. *
  895. * OR
  896. *
  897. * Input:
  898. * buf: C string containing the pathname
  899. * of the directory on a local file
  900. * system containing permanent NFSv4
  901. * recovery data
  902. * size: non-zero length of C string in @buf
  903. * Output:
  904. * On success: passed-in buffer filled with '\n'-terminated C string
  905. * containing the current recovery pathname setting;
  906. * return code is the size in bytes of the string
  907. * On error: return code is zero or a negative errno value
  908. */
  909. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  910. {
  911. ssize_t rv;
  912. struct net *net = file->f_dentry->d_sb->s_fs_info;
  913. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  914. mutex_lock(&nfsd_mutex);
  915. rv = __write_recoverydir(file, buf, size, nn);
  916. mutex_unlock(&nfsd_mutex);
  917. return rv;
  918. }
  919. #endif
  920. /*----------------------------------------------------------------------------*/
  921. /*
  922. * populating the filesystem.
  923. */
  924. static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
  925. {
  926. static struct tree_descr nfsd_files[] = {
  927. [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
  928. [NFSD_Export_features] = {"export_features",
  929. &export_features_operations, S_IRUGO},
  930. [NFSD_FO_UnlockIP] = {"unlock_ip",
  931. &transaction_ops, S_IWUSR|S_IRUSR},
  932. [NFSD_FO_UnlockFS] = {"unlock_filesystem",
  933. &transaction_ops, S_IWUSR|S_IRUSR},
  934. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  935. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  936. [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
  937. [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
  938. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  939. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  940. [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
  941. #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
  942. [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
  943. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  944. #ifdef CONFIG_NFSD_V4
  945. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  946. [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
  947. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  948. #endif
  949. /* last one */ {""}
  950. };
  951. struct net *net = data;
  952. int ret;
  953. ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
  954. if (ret)
  955. return ret;
  956. sb->s_fs_info = get_net(net);
  957. return 0;
  958. }
  959. static struct dentry *nfsd_mount(struct file_system_type *fs_type,
  960. int flags, const char *dev_name, void *data)
  961. {
  962. return mount_ns(fs_type, flags, current->nsproxy->net_ns, nfsd_fill_super);
  963. }
  964. static void nfsd_umount(struct super_block *sb)
  965. {
  966. struct net *net = sb->s_fs_info;
  967. kill_litter_super(sb);
  968. put_net(net);
  969. }
  970. static struct file_system_type nfsd_fs_type = {
  971. .owner = THIS_MODULE,
  972. .name = "nfsd",
  973. .mount = nfsd_mount,
  974. .kill_sb = nfsd_umount,
  975. };
  976. MODULE_ALIAS_FS("nfsd");
  977. #ifdef CONFIG_PROC_FS
  978. static int create_proc_exports_entry(void)
  979. {
  980. struct proc_dir_entry *entry;
  981. entry = proc_mkdir("fs/nfs", NULL);
  982. if (!entry)
  983. return -ENOMEM;
  984. entry = proc_create("exports", 0, entry,
  985. &exports_proc_operations);
  986. if (!entry)
  987. return -ENOMEM;
  988. return 0;
  989. }
  990. #else /* CONFIG_PROC_FS */
  991. static int create_proc_exports_entry(void)
  992. {
  993. return 0;
  994. }
  995. #endif
  996. int nfsd_net_id;
  997. static __net_init int nfsd_init_net(struct net *net)
  998. {
  999. int retval;
  1000. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1001. retval = nfsd_export_init(net);
  1002. if (retval)
  1003. goto out_export_error;
  1004. retval = nfsd_idmap_init(net);
  1005. if (retval)
  1006. goto out_idmap_error;
  1007. nn->nfsd4_lease = 90; /* default lease time */
  1008. nn->nfsd4_grace = 90;
  1009. return 0;
  1010. out_idmap_error:
  1011. nfsd_export_shutdown(net);
  1012. out_export_error:
  1013. return retval;
  1014. }
  1015. static __net_exit void nfsd_exit_net(struct net *net)
  1016. {
  1017. nfsd_idmap_shutdown(net);
  1018. nfsd_export_shutdown(net);
  1019. }
  1020. static struct pernet_operations nfsd_net_ops = {
  1021. .init = nfsd_init_net,
  1022. .exit = nfsd_exit_net,
  1023. .id = &nfsd_net_id,
  1024. .size = sizeof(struct nfsd_net),
  1025. };
  1026. static int __init init_nfsd(void)
  1027. {
  1028. int retval;
  1029. printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
  1030. retval = register_cld_notifier();
  1031. if (retval)
  1032. return retval;
  1033. retval = register_pernet_subsys(&nfsd_net_ops);
  1034. if (retval < 0)
  1035. goto out_unregister_notifier;
  1036. retval = nfsd4_init_slabs();
  1037. if (retval)
  1038. goto out_unregister_pernet;
  1039. nfs4_state_init();
  1040. retval = nfsd_fault_inject_init(); /* nfsd fault injection controls */
  1041. if (retval)
  1042. goto out_free_slabs;
  1043. nfsd_stat_init(); /* Statistics */
  1044. retval = nfsd_reply_cache_init();
  1045. if (retval)
  1046. goto out_free_stat;
  1047. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  1048. retval = create_proc_exports_entry();
  1049. if (retval)
  1050. goto out_free_lockd;
  1051. retval = register_filesystem(&nfsd_fs_type);
  1052. if (retval)
  1053. goto out_free_all;
  1054. return 0;
  1055. out_free_all:
  1056. remove_proc_entry("fs/nfs/exports", NULL);
  1057. remove_proc_entry("fs/nfs", NULL);
  1058. out_free_lockd:
  1059. nfsd_lockd_shutdown();
  1060. nfsd_reply_cache_shutdown();
  1061. out_free_stat:
  1062. nfsd_stat_shutdown();
  1063. nfsd_fault_inject_cleanup();
  1064. out_free_slabs:
  1065. nfsd4_free_slabs();
  1066. out_unregister_pernet:
  1067. unregister_pernet_subsys(&nfsd_net_ops);
  1068. out_unregister_notifier:
  1069. unregister_cld_notifier();
  1070. return retval;
  1071. }
  1072. static void __exit exit_nfsd(void)
  1073. {
  1074. nfsd_reply_cache_shutdown();
  1075. remove_proc_entry("fs/nfs/exports", NULL);
  1076. remove_proc_entry("fs/nfs", NULL);
  1077. nfsd_stat_shutdown();
  1078. nfsd_lockd_shutdown();
  1079. nfsd4_free_slabs();
  1080. nfsd_fault_inject_cleanup();
  1081. unregister_filesystem(&nfsd_fs_type);
  1082. unregister_pernet_subsys(&nfsd_net_ops);
  1083. unregister_cld_notifier();
  1084. }
  1085. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1086. MODULE_LICENSE("GPL");
  1087. module_init(init_nfsd)
  1088. module_exit(exit_nfsd)