nfsctl.c 33 KB

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