nfsctl.c 29 KB

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