nfsctl.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. NFSD_V4EndGrace,
  49. #endif
  50. };
  51. /*
  52. * write() for these nodes.
  53. */
  54. static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
  55. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
  56. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
  57. static ssize_t write_threads(struct file *file, char *buf, size_t size);
  58. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
  59. static ssize_t write_versions(struct file *file, char *buf, size_t size);
  60. static ssize_t write_ports(struct file *file, char *buf, size_t size);
  61. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
  62. static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
  63. #ifdef CONFIG_NFSD_V4
  64. static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
  65. static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
  66. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
  67. static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
  68. #endif
  69. static ssize_t (*write_op[])(struct file *, char *, size_t) = {
  70. [NFSD_Fh] = write_filehandle,
  71. [NFSD_FO_UnlockIP] = write_unlock_ip,
  72. [NFSD_FO_UnlockFS] = write_unlock_fs,
  73. [NFSD_Threads] = write_threads,
  74. [NFSD_Pool_Threads] = write_pool_threads,
  75. [NFSD_Versions] = write_versions,
  76. [NFSD_Ports] = write_ports,
  77. [NFSD_MaxBlkSize] = write_maxblksize,
  78. [NFSD_MaxConnections] = write_maxconn,
  79. #ifdef CONFIG_NFSD_V4
  80. [NFSD_Leasetime] = write_leasetime,
  81. [NFSD_Gracetime] = write_gracetime,
  82. [NFSD_RecoveryDir] = write_recoverydir,
  83. [NFSD_V4EndGrace] = write_v4_end_grace,
  84. #endif
  85. };
  86. static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  87. {
  88. ino_t ino = file_inode(file)->i_ino;
  89. char *data;
  90. ssize_t rv;
  91. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  92. return -EINVAL;
  93. data = simple_transaction_get(file, buf, size);
  94. if (IS_ERR(data))
  95. return PTR_ERR(data);
  96. rv = write_op[ino](file, data, size);
  97. if (rv >= 0) {
  98. simple_transaction_set(file, rv);
  99. rv = size;
  100. }
  101. return rv;
  102. }
  103. static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  104. {
  105. if (! file->private_data) {
  106. /* An attempt to read a transaction file without writing
  107. * causes a 0-byte write so that the file can return
  108. * state information
  109. */
  110. ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
  111. if (rv < 0)
  112. return rv;
  113. }
  114. return simple_transaction_read(file, buf, size, pos);
  115. }
  116. static const struct file_operations transaction_ops = {
  117. .write = nfsctl_transaction_write,
  118. .read = nfsctl_transaction_read,
  119. .release = simple_transaction_release,
  120. .llseek = default_llseek,
  121. };
  122. static int exports_net_open(struct net *net, struct file *file)
  123. {
  124. int err;
  125. struct seq_file *seq;
  126. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  127. err = seq_open(file, &nfs_exports_op);
  128. if (err)
  129. return err;
  130. seq = file->private_data;
  131. seq->private = nn->svc_export_cache;
  132. return 0;
  133. }
  134. static int exports_proc_open(struct inode *inode, struct file *file)
  135. {
  136. return exports_net_open(current->nsproxy->net_ns, file);
  137. }
  138. static const struct file_operations exports_proc_operations = {
  139. .open = exports_proc_open,
  140. .read = seq_read,
  141. .llseek = seq_lseek,
  142. .release = seq_release,
  143. .owner = THIS_MODULE,
  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. .owner = THIS_MODULE,
  155. };
  156. static int export_features_show(struct seq_file *m, void *v)
  157. {
  158. seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
  159. return 0;
  160. }
  161. static int export_features_open(struct inode *inode, struct file *file)
  162. {
  163. return single_open(file, export_features_show, NULL);
  164. }
  165. static const struct file_operations export_features_operations = {
  166. .open = export_features_open,
  167. .read = seq_read,
  168. .llseek = seq_lseek,
  169. .release = single_release,
  170. };
  171. #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
  172. static int supported_enctypes_show(struct seq_file *m, void *v)
  173. {
  174. seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
  175. return 0;
  176. }
  177. static int supported_enctypes_open(struct inode *inode, struct file *file)
  178. {
  179. return single_open(file, supported_enctypes_show, NULL);
  180. }
  181. static const struct file_operations supported_enctypes_ops = {
  182. .open = supported_enctypes_open,
  183. .read = seq_read,
  184. .llseek = seq_lseek,
  185. .release = single_release,
  186. };
  187. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  188. static const struct file_operations pool_stats_operations = {
  189. .open = nfsd_pool_stats_open,
  190. .read = seq_read,
  191. .llseek = seq_lseek,
  192. .release = nfsd_pool_stats_release,
  193. .owner = THIS_MODULE,
  194. };
  195. static struct file_operations reply_cache_stats_operations = {
  196. .open = nfsd_reply_cache_stats_open,
  197. .read = seq_read,
  198. .llseek = seq_lseek,
  199. .release = single_release,
  200. };
  201. /*----------------------------------------------------------------------------*/
  202. /*
  203. * payload - write methods
  204. */
  205. /**
  206. * write_unlock_ip - Release all locks used by a client
  207. *
  208. * Experimental.
  209. *
  210. * Input:
  211. * buf: '\n'-terminated C string containing a
  212. * presentation format IP address
  213. * size: length of C string in @buf
  214. * Output:
  215. * On success: returns zero if all specified locks were released;
  216. * returns one if one or more locks were not released
  217. * On error: return code is negative errno value
  218. */
  219. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
  220. {
  221. struct sockaddr_storage address;
  222. struct sockaddr *sap = (struct sockaddr *)&address;
  223. size_t salen = sizeof(address);
  224. char *fo_path;
  225. struct net *net = file->f_dentry->d_sb->s_fs_info;
  226. /* sanity check */
  227. if (size == 0)
  228. return -EINVAL;
  229. if (buf[size-1] != '\n')
  230. return -EINVAL;
  231. fo_path = buf;
  232. if (qword_get(&buf, fo_path, size) < 0)
  233. return -EINVAL;
  234. if (rpc_pton(net, fo_path, size, sap, salen) == 0)
  235. return -EINVAL;
  236. return nlmsvc_unlock_all_by_ip(sap);
  237. }
  238. /**
  239. * write_unlock_fs - Release all locks on a local file system
  240. *
  241. * Experimental.
  242. *
  243. * Input:
  244. * buf: '\n'-terminated C string containing the
  245. * absolute pathname of a local file system
  246. * size: length of C string in @buf
  247. * Output:
  248. * On success: returns zero if all specified locks were released;
  249. * returns one if one or more locks were not released
  250. * On error: return code is negative errno value
  251. */
  252. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
  253. {
  254. struct path path;
  255. char *fo_path;
  256. int error;
  257. /* sanity check */
  258. if (size == 0)
  259. return -EINVAL;
  260. if (buf[size-1] != '\n')
  261. return -EINVAL;
  262. fo_path = buf;
  263. if (qword_get(&buf, fo_path, size) < 0)
  264. return -EINVAL;
  265. error = kern_path(fo_path, 0, &path);
  266. if (error)
  267. return error;
  268. /*
  269. * XXX: Needs better sanity checking. Otherwise we could end up
  270. * releasing locks on the wrong file system.
  271. *
  272. * For example:
  273. * 1. Does the path refer to a directory?
  274. * 2. Is that directory a mount point, or
  275. * 3. Is that directory the root of an exported file system?
  276. */
  277. error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
  278. path_put(&path);
  279. return error;
  280. }
  281. /**
  282. * write_filehandle - Get a variable-length NFS file handle by path
  283. *
  284. * On input, the buffer contains a '\n'-terminated C string comprised of
  285. * three alphanumeric words separated by whitespace. The string may
  286. * contain escape sequences.
  287. *
  288. * Input:
  289. * buf:
  290. * domain: client domain name
  291. * path: export pathname
  292. * maxsize: numeric maximum size of
  293. * @buf
  294. * size: length of C string in @buf
  295. * Output:
  296. * On success: passed-in buffer filled with '\n'-terminated C
  297. * string containing a ASCII hex text version
  298. * of the NFS file handle;
  299. * return code is the size in bytes of the string
  300. * On error: return code is negative errno value
  301. */
  302. static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
  303. {
  304. char *dname, *path;
  305. int uninitialized_var(maxsize);
  306. char *mesg = buf;
  307. int len;
  308. struct auth_domain *dom;
  309. struct knfsd_fh fh;
  310. struct net *net = file->f_dentry->d_sb->s_fs_info;
  311. if (size == 0)
  312. return -EINVAL;
  313. if (buf[size-1] != '\n')
  314. return -EINVAL;
  315. buf[size-1] = 0;
  316. dname = mesg;
  317. len = qword_get(&mesg, dname, size);
  318. if (len <= 0)
  319. return -EINVAL;
  320. path = dname+len+1;
  321. len = qword_get(&mesg, path, size);
  322. if (len <= 0)
  323. return -EINVAL;
  324. len = get_int(&mesg, &maxsize);
  325. if (len)
  326. return len;
  327. if (maxsize < NFS_FHSIZE)
  328. return -EINVAL;
  329. maxsize = min(maxsize, NFS3_FHSIZE);
  330. if (qword_get(&mesg, mesg, size)>0)
  331. return -EINVAL;
  332. /* we have all the words, they are in buf.. */
  333. dom = unix_domain_find(dname);
  334. if (!dom)
  335. return -ENOMEM;
  336. len = exp_rootfh(net, dom, path, &fh, maxsize);
  337. auth_domain_put(dom);
  338. if (len)
  339. return len;
  340. mesg = buf;
  341. len = SIMPLE_TRANSACTION_LIMIT;
  342. qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
  343. mesg[-1] = '\n';
  344. return mesg - buf;
  345. }
  346. /**
  347. * write_threads - Start NFSD, or report the current number of running threads
  348. *
  349. * Input:
  350. * buf: ignored
  351. * size: zero
  352. * Output:
  353. * On success: passed-in buffer filled with '\n'-terminated C
  354. * string numeric value representing the number of
  355. * running NFSD threads;
  356. * return code is the size in bytes of the string
  357. * On error: return code is zero
  358. *
  359. * OR
  360. *
  361. * Input:
  362. * buf: C string containing an unsigned
  363. * integer value representing the
  364. * number of NFSD threads to start
  365. * size: non-zero length of C string in @buf
  366. * Output:
  367. * On success: NFS service is started;
  368. * passed-in buffer filled with '\n'-terminated C
  369. * string numeric value representing the number of
  370. * running NFSD threads;
  371. * return code is the size in bytes of the string
  372. * On error: return code is zero or a negative errno value
  373. */
  374. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  375. {
  376. char *mesg = buf;
  377. int rv;
  378. struct net *net = file->f_dentry->d_sb->s_fs_info;
  379. if (size > 0) {
  380. int newthreads;
  381. rv = get_int(&mesg, &newthreads);
  382. if (rv)
  383. return rv;
  384. if (newthreads < 0)
  385. return -EINVAL;
  386. rv = nfsd_svc(newthreads, net);
  387. if (rv < 0)
  388. return rv;
  389. } else
  390. rv = nfsd_nrthreads(net);
  391. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
  392. }
  393. /**
  394. * write_pool_threads - Set or report the current number of threads per pool
  395. *
  396. * Input:
  397. * buf: ignored
  398. * size: zero
  399. *
  400. * OR
  401. *
  402. * Input:
  403. * buf: C string containing whitespace-
  404. * separated unsigned integer values
  405. * representing the number of NFSD
  406. * threads to start in each pool
  407. * size: non-zero length of C string in @buf
  408. * Output:
  409. * On success: passed-in buffer filled with '\n'-terminated C
  410. * string containing integer values representing the
  411. * number of NFSD threads in each pool;
  412. * return code is the size in bytes of the string
  413. * On error: return code is zero or a negative errno value
  414. */
  415. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
  416. {
  417. /* if size > 0, look for an array of number of threads per node
  418. * and apply them then write out number of threads per node as reply
  419. */
  420. char *mesg = buf;
  421. int i;
  422. int rv;
  423. int len;
  424. int npools;
  425. int *nthreads;
  426. struct net *net = file->f_dentry->d_sb->s_fs_info;
  427. mutex_lock(&nfsd_mutex);
  428. npools = nfsd_nrpools(net);
  429. if (npools == 0) {
  430. /*
  431. * NFS is shut down. The admin can start it by
  432. * writing to the threads file but NOT the pool_threads
  433. * file, sorry. Report zero threads.
  434. */
  435. mutex_unlock(&nfsd_mutex);
  436. strcpy(buf, "0\n");
  437. return strlen(buf);
  438. }
  439. nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
  440. rv = -ENOMEM;
  441. if (nthreads == NULL)
  442. goto out_free;
  443. if (size > 0) {
  444. for (i = 0; i < npools; i++) {
  445. rv = get_int(&mesg, &nthreads[i]);
  446. if (rv == -ENOENT)
  447. break; /* fewer numbers than pools */
  448. if (rv)
  449. goto out_free; /* syntax error */
  450. rv = -EINVAL;
  451. if (nthreads[i] < 0)
  452. goto out_free;
  453. }
  454. rv = nfsd_set_nrthreads(i, nthreads, net);
  455. if (rv)
  456. goto out_free;
  457. }
  458. rv = nfsd_get_nrthreads(npools, nthreads, net);
  459. if (rv)
  460. goto out_free;
  461. mesg = buf;
  462. size = SIMPLE_TRANSACTION_LIMIT;
  463. for (i = 0; i < npools && size > 0; i++) {
  464. snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
  465. len = strlen(mesg);
  466. size -= len;
  467. mesg += len;
  468. }
  469. rv = mesg - buf;
  470. out_free:
  471. kfree(nthreads);
  472. mutex_unlock(&nfsd_mutex);
  473. return rv;
  474. }
  475. static ssize_t __write_versions(struct file *file, char *buf, size_t size)
  476. {
  477. char *mesg = buf;
  478. char *vers, *minorp, sign;
  479. int len, num, remaining;
  480. unsigned minor;
  481. ssize_t tlen = 0;
  482. char *sep;
  483. struct net *net = file->f_dentry->d_sb->s_fs_info;
  484. struct nfsd_net *nn = net_generic(net, 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. struct net *net = file->f_dentry->d_sb->s_fs_info;
  743. mutex_lock(&nfsd_mutex);
  744. rv = __write_ports(file, buf, size, net);
  745. mutex_unlock(&nfsd_mutex);
  746. return rv;
  747. }
  748. int nfsd_max_blksize;
  749. /**
  750. * write_maxblksize - Set or report the current NFS blksize
  751. *
  752. * Input:
  753. * buf: ignored
  754. * size: zero
  755. *
  756. * OR
  757. *
  758. * Input:
  759. * buf: C string containing an unsigned
  760. * integer value representing the new
  761. * NFS blksize
  762. * size: non-zero length of C string in @buf
  763. * Output:
  764. * On success: passed-in buffer filled with '\n'-terminated C string
  765. * containing numeric value of the current NFS blksize
  766. * setting;
  767. * return code is the size in bytes of the string
  768. * On error: return code is zero or a negative errno value
  769. */
  770. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
  771. {
  772. char *mesg = buf;
  773. struct net *net = file->f_dentry->d_sb->s_fs_info;
  774. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  775. if (size > 0) {
  776. int bsize;
  777. int rv = get_int(&mesg, &bsize);
  778. if (rv)
  779. return rv;
  780. /* force bsize into allowed range and
  781. * required alignment.
  782. */
  783. bsize = max_t(int, bsize, 1024);
  784. bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
  785. bsize &= ~(1024-1);
  786. mutex_lock(&nfsd_mutex);
  787. if (nn->nfsd_serv) {
  788. mutex_unlock(&nfsd_mutex);
  789. return -EBUSY;
  790. }
  791. nfsd_max_blksize = bsize;
  792. mutex_unlock(&nfsd_mutex);
  793. }
  794. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
  795. nfsd_max_blksize);
  796. }
  797. /**
  798. * write_maxconn - Set or report the current max number of connections
  799. *
  800. * Input:
  801. * buf: ignored
  802. * size: zero
  803. * OR
  804. *
  805. * Input:
  806. * buf: C string containing an unsigned
  807. * integer value representing the new
  808. * number of max connections
  809. * size: non-zero length of C string in @buf
  810. * Output:
  811. * On success: passed-in buffer filled with '\n'-terminated C string
  812. * containing numeric value of max_connections setting
  813. * for this net namespace;
  814. * return code is the size in bytes of the string
  815. * On error: return code is zero or a negative errno value
  816. */
  817. static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
  818. {
  819. char *mesg = buf;
  820. struct net *net = file->f_dentry->d_sb->s_fs_info;
  821. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  822. unsigned int maxconn = nn->max_connections;
  823. if (size > 0) {
  824. int rv = get_uint(&mesg, &maxconn);
  825. if (rv)
  826. return rv;
  827. nn->max_connections = maxconn;
  828. }
  829. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
  830. }
  831. #ifdef CONFIG_NFSD_V4
  832. static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
  833. time_t *time, struct nfsd_net *nn)
  834. {
  835. char *mesg = buf;
  836. int rv, i;
  837. if (size > 0) {
  838. if (nn->nfsd_serv)
  839. return -EBUSY;
  840. rv = get_int(&mesg, &i);
  841. if (rv)
  842. return rv;
  843. /*
  844. * Some sanity checking. We don't have a reason for
  845. * these particular numbers, but problems with the
  846. * extremes are:
  847. * - Too short: the briefest network outage may
  848. * cause clients to lose all their locks. Also,
  849. * the frequent polling may be wasteful.
  850. * - Too long: do you really want reboot recovery
  851. * to take more than an hour? Or to make other
  852. * clients wait an hour before being able to
  853. * revoke a dead client's locks?
  854. */
  855. if (i < 10 || i > 3600)
  856. return -EINVAL;
  857. *time = i;
  858. }
  859. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
  860. }
  861. static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
  862. time_t *time, struct nfsd_net *nn)
  863. {
  864. ssize_t rv;
  865. mutex_lock(&nfsd_mutex);
  866. rv = __nfsd4_write_time(file, buf, size, time, nn);
  867. mutex_unlock(&nfsd_mutex);
  868. return rv;
  869. }
  870. /**
  871. * write_leasetime - Set or report the current NFSv4 lease time
  872. *
  873. * Input:
  874. * buf: ignored
  875. * size: zero
  876. *
  877. * OR
  878. *
  879. * Input:
  880. * buf: C string containing an unsigned
  881. * integer value representing the new
  882. * NFSv4 lease expiry time
  883. * size: non-zero length of C string in @buf
  884. * Output:
  885. * On success: passed-in buffer filled with '\n'-terminated C
  886. * string containing unsigned integer value of the
  887. * current lease expiry time;
  888. * return code is the size in bytes of the string
  889. * On error: return code is zero or a negative errno value
  890. */
  891. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  892. {
  893. struct net *net = file->f_dentry->d_sb->s_fs_info;
  894. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  895. return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
  896. }
  897. /**
  898. * write_gracetime - Set or report current NFSv4 grace period time
  899. *
  900. * As above, but sets the time of the NFSv4 grace period.
  901. *
  902. * Note this should never be set to less than the *previous*
  903. * lease-period time, but we don't try to enforce this. (In the common
  904. * case (a new boot), we don't know what the previous lease time was
  905. * anyway.)
  906. */
  907. static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
  908. {
  909. struct net *net = file->f_dentry->d_sb->s_fs_info;
  910. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  911. return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
  912. }
  913. static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
  914. struct nfsd_net *nn)
  915. {
  916. char *mesg = buf;
  917. char *recdir;
  918. int len, status;
  919. if (size > 0) {
  920. if (nn->nfsd_serv)
  921. return -EBUSY;
  922. if (size > PATH_MAX || buf[size-1] != '\n')
  923. return -EINVAL;
  924. buf[size-1] = 0;
  925. recdir = mesg;
  926. len = qword_get(&mesg, recdir, size);
  927. if (len <= 0)
  928. return -EINVAL;
  929. status = nfs4_reset_recoverydir(recdir);
  930. if (status)
  931. return status;
  932. }
  933. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
  934. nfs4_recoverydir());
  935. }
  936. /**
  937. * write_recoverydir - Set or report the pathname of the recovery directory
  938. *
  939. * Input:
  940. * buf: ignored
  941. * size: zero
  942. *
  943. * OR
  944. *
  945. * Input:
  946. * buf: C string containing the pathname
  947. * of the directory on a local file
  948. * system containing permanent NFSv4
  949. * recovery data
  950. * size: non-zero length of C string in @buf
  951. * Output:
  952. * On success: passed-in buffer filled with '\n'-terminated C string
  953. * containing the current recovery pathname setting;
  954. * return code is the size in bytes of the string
  955. * On error: return code is zero or a negative errno value
  956. */
  957. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  958. {
  959. ssize_t rv;
  960. struct net *net = file->f_dentry->d_sb->s_fs_info;
  961. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  962. mutex_lock(&nfsd_mutex);
  963. rv = __write_recoverydir(file, buf, size, nn);
  964. mutex_unlock(&nfsd_mutex);
  965. return rv;
  966. }
  967. /**
  968. * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
  969. *
  970. * Input:
  971. * buf: ignored
  972. * size: zero
  973. * OR
  974. *
  975. * Input:
  976. * buf: any value
  977. * size: non-zero length of C string in @buf
  978. * Output:
  979. * passed-in buffer filled with "Y" or "N" with a newline
  980. * and NULL-terminated C string. This indicates whether
  981. * the grace period has ended in the current net
  982. * namespace. Return code is the size in bytes of the
  983. * string. Writing a string that starts with 'Y', 'y', or
  984. * '1' to the file will end the grace period for nfsd's v4
  985. * lock manager.
  986. */
  987. static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
  988. {
  989. struct net *net = file->f_dentry->d_sb->s_fs_info;
  990. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  991. if (size > 0) {
  992. switch(buf[0]) {
  993. case 'Y':
  994. case 'y':
  995. case '1':
  996. nfsd4_end_grace(nn);
  997. break;
  998. default:
  999. return -EINVAL;
  1000. }
  1001. }
  1002. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
  1003. nn->grace_ended ? 'Y' : 'N');
  1004. }
  1005. #endif
  1006. /*----------------------------------------------------------------------------*/
  1007. /*
  1008. * populating the filesystem.
  1009. */
  1010. static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
  1011. {
  1012. static struct tree_descr nfsd_files[] = {
  1013. [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
  1014. [NFSD_Export_features] = {"export_features",
  1015. &export_features_operations, S_IRUGO},
  1016. [NFSD_FO_UnlockIP] = {"unlock_ip",
  1017. &transaction_ops, S_IWUSR|S_IRUSR},
  1018. [NFSD_FO_UnlockFS] = {"unlock_filesystem",
  1019. &transaction_ops, S_IWUSR|S_IRUSR},
  1020. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  1021. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  1022. [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
  1023. [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
  1024. [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", &reply_cache_stats_operations, S_IRUGO},
  1025. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  1026. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  1027. [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
  1028. [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
  1029. #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
  1030. [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
  1031. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  1032. #ifdef CONFIG_NFSD_V4
  1033. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  1034. [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
  1035. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  1036. [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
  1037. #endif
  1038. /* last one */ {""}
  1039. };
  1040. struct net *net = data;
  1041. int ret;
  1042. ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
  1043. if (ret)
  1044. return ret;
  1045. sb->s_fs_info = get_net(net);
  1046. return 0;
  1047. }
  1048. static struct dentry *nfsd_mount(struct file_system_type *fs_type,
  1049. int flags, const char *dev_name, void *data)
  1050. {
  1051. return mount_ns(fs_type, flags, current->nsproxy->net_ns, nfsd_fill_super);
  1052. }
  1053. static void nfsd_umount(struct super_block *sb)
  1054. {
  1055. struct net *net = sb->s_fs_info;
  1056. kill_litter_super(sb);
  1057. put_net(net);
  1058. }
  1059. static struct file_system_type nfsd_fs_type = {
  1060. .owner = THIS_MODULE,
  1061. .name = "nfsd",
  1062. .mount = nfsd_mount,
  1063. .kill_sb = nfsd_umount,
  1064. };
  1065. MODULE_ALIAS_FS("nfsd");
  1066. #ifdef CONFIG_PROC_FS
  1067. static int create_proc_exports_entry(void)
  1068. {
  1069. struct proc_dir_entry *entry;
  1070. entry = proc_mkdir("fs/nfs", NULL);
  1071. if (!entry)
  1072. return -ENOMEM;
  1073. entry = proc_create("exports", 0, entry,
  1074. &exports_proc_operations);
  1075. if (!entry) {
  1076. remove_proc_entry("fs/nfs", NULL);
  1077. return -ENOMEM;
  1078. }
  1079. return 0;
  1080. }
  1081. #else /* CONFIG_PROC_FS */
  1082. static int create_proc_exports_entry(void)
  1083. {
  1084. return 0;
  1085. }
  1086. #endif
  1087. int nfsd_net_id;
  1088. static __net_init int nfsd_init_net(struct net *net)
  1089. {
  1090. int retval;
  1091. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1092. retval = nfsd_export_init(net);
  1093. if (retval)
  1094. goto out_export_error;
  1095. retval = nfsd_idmap_init(net);
  1096. if (retval)
  1097. goto out_idmap_error;
  1098. nn->nfsd4_lease = 90; /* default lease time */
  1099. nn->nfsd4_grace = 90;
  1100. return 0;
  1101. out_idmap_error:
  1102. nfsd_export_shutdown(net);
  1103. out_export_error:
  1104. return retval;
  1105. }
  1106. static __net_exit void nfsd_exit_net(struct net *net)
  1107. {
  1108. nfsd_idmap_shutdown(net);
  1109. nfsd_export_shutdown(net);
  1110. }
  1111. static struct pernet_operations nfsd_net_ops = {
  1112. .init = nfsd_init_net,
  1113. .exit = nfsd_exit_net,
  1114. .id = &nfsd_net_id,
  1115. .size = sizeof(struct nfsd_net),
  1116. };
  1117. static int __init init_nfsd(void)
  1118. {
  1119. int retval;
  1120. printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
  1121. retval = register_cld_notifier();
  1122. if (retval)
  1123. return retval;
  1124. retval = register_pernet_subsys(&nfsd_net_ops);
  1125. if (retval < 0)
  1126. goto out_unregister_notifier;
  1127. retval = nfsd4_init_slabs();
  1128. if (retval)
  1129. goto out_unregister_pernet;
  1130. retval = nfsd_fault_inject_init(); /* nfsd fault injection controls */
  1131. if (retval)
  1132. goto out_free_slabs;
  1133. nfsd_stat_init(); /* Statistics */
  1134. retval = nfsd_reply_cache_init();
  1135. if (retval)
  1136. goto out_free_stat;
  1137. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  1138. retval = create_proc_exports_entry();
  1139. if (retval)
  1140. goto out_free_lockd;
  1141. retval = register_filesystem(&nfsd_fs_type);
  1142. if (retval)
  1143. goto out_free_all;
  1144. return 0;
  1145. out_free_all:
  1146. remove_proc_entry("fs/nfs/exports", NULL);
  1147. remove_proc_entry("fs/nfs", NULL);
  1148. out_free_lockd:
  1149. nfsd_lockd_shutdown();
  1150. nfsd_reply_cache_shutdown();
  1151. out_free_stat:
  1152. nfsd_stat_shutdown();
  1153. nfsd_fault_inject_cleanup();
  1154. out_free_slabs:
  1155. nfsd4_free_slabs();
  1156. out_unregister_pernet:
  1157. unregister_pernet_subsys(&nfsd_net_ops);
  1158. out_unregister_notifier:
  1159. unregister_cld_notifier();
  1160. return retval;
  1161. }
  1162. static void __exit exit_nfsd(void)
  1163. {
  1164. nfsd_reply_cache_shutdown();
  1165. remove_proc_entry("fs/nfs/exports", NULL);
  1166. remove_proc_entry("fs/nfs", NULL);
  1167. nfsd_stat_shutdown();
  1168. nfsd_lockd_shutdown();
  1169. nfsd4_free_slabs();
  1170. nfsd_fault_inject_cleanup();
  1171. unregister_filesystem(&nfsd_fs_type);
  1172. unregister_pernet_subsys(&nfsd_net_ops);
  1173. unregister_cld_notifier();
  1174. }
  1175. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1176. MODULE_LICENSE("GPL");
  1177. module_init(init_nfsd)
  1178. module_exit(exit_nfsd)