nfsctl.c 29 KB

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