nfsctl.c 32 KB

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