nfsctl.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Syscall interface to knfsd.
  4. *
  5. * Copyright (C) 1995, 1996 Olaf Kirch <[email protected]>
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/namei.h>
  9. #include <linux/ctype.h>
  10. #include <linux/fs_context.h>
  11. #include <linux/sunrpc/svcsock.h>
  12. #include <linux/lockd/lockd.h>
  13. #include <linux/sunrpc/addr.h>
  14. #include <linux/sunrpc/gss_api.h>
  15. #include <linux/sunrpc/gss_krb5_enctypes.h>
  16. #include <linux/sunrpc/rpc_pipe_fs.h>
  17. #include <linux/module.h>
  18. #include <linux/fsnotify.h>
  19. #include "idmap.h"
  20. #include "nfsd.h"
  21. #include "cache.h"
  22. #include "state.h"
  23. #include "netns.h"
  24. #include "pnfs.h"
  25. #include "filecache.h"
  26. /*
  27. * We have a single directory with several nodes in it.
  28. */
  29. enum {
  30. NFSD_Root = 1,
  31. NFSD_List,
  32. NFSD_Export_Stats,
  33. NFSD_Export_features,
  34. NFSD_Fh,
  35. NFSD_FO_UnlockIP,
  36. NFSD_FO_UnlockFS,
  37. NFSD_Threads,
  38. NFSD_Pool_Threads,
  39. NFSD_Pool_Stats,
  40. NFSD_Reply_Cache_Stats,
  41. NFSD_Versions,
  42. NFSD_Ports,
  43. NFSD_MaxBlkSize,
  44. NFSD_MaxConnections,
  45. NFSD_Filecache,
  46. NFSD_SupportedEnctypes,
  47. /*
  48. * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
  49. * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
  50. */
  51. #ifdef CONFIG_NFSD_V4
  52. NFSD_Leasetime,
  53. NFSD_Gracetime,
  54. NFSD_RecoveryDir,
  55. NFSD_V4EndGrace,
  56. #endif
  57. NFSD_MaxReserved
  58. };
  59. /*
  60. * write() for these nodes.
  61. */
  62. static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
  63. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
  64. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
  65. static ssize_t write_threads(struct file *file, char *buf, size_t size);
  66. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
  67. static ssize_t write_versions(struct file *file, char *buf, size_t size);
  68. static ssize_t write_ports(struct file *file, char *buf, size_t size);
  69. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
  70. static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
  71. #ifdef CONFIG_NFSD_V4
  72. static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
  73. static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
  74. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
  75. static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
  76. #endif
  77. static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
  78. [NFSD_Fh] = write_filehandle,
  79. [NFSD_FO_UnlockIP] = write_unlock_ip,
  80. [NFSD_FO_UnlockFS] = write_unlock_fs,
  81. [NFSD_Threads] = write_threads,
  82. [NFSD_Pool_Threads] = write_pool_threads,
  83. [NFSD_Versions] = write_versions,
  84. [NFSD_Ports] = write_ports,
  85. [NFSD_MaxBlkSize] = write_maxblksize,
  86. [NFSD_MaxConnections] = write_maxconn,
  87. #ifdef CONFIG_NFSD_V4
  88. [NFSD_Leasetime] = write_leasetime,
  89. [NFSD_Gracetime] = write_gracetime,
  90. [NFSD_RecoveryDir] = write_recoverydir,
  91. [NFSD_V4EndGrace] = write_v4_end_grace,
  92. #endif
  93. };
  94. static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
  95. {
  96. ino_t ino = file_inode(file)->i_ino;
  97. char *data;
  98. ssize_t rv;
  99. if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
  100. return -EINVAL;
  101. data = simple_transaction_get(file, buf, size);
  102. if (IS_ERR(data))
  103. return PTR_ERR(data);
  104. rv = write_op[ino](file, data, size);
  105. if (rv >= 0) {
  106. simple_transaction_set(file, rv);
  107. rv = size;
  108. }
  109. return rv;
  110. }
  111. static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  112. {
  113. if (! file->private_data) {
  114. /* An attempt to read a transaction file without writing
  115. * causes a 0-byte write so that the file can return
  116. * state information
  117. */
  118. ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
  119. if (rv < 0)
  120. return rv;
  121. }
  122. return simple_transaction_read(file, buf, size, pos);
  123. }
  124. static const struct file_operations transaction_ops = {
  125. .write = nfsctl_transaction_write,
  126. .read = nfsctl_transaction_read,
  127. .release = simple_transaction_release,
  128. .llseek = default_llseek,
  129. };
  130. static int exports_net_open(struct net *net, struct file *file)
  131. {
  132. int err;
  133. struct seq_file *seq;
  134. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  135. err = seq_open(file, &nfs_exports_op);
  136. if (err)
  137. return err;
  138. seq = file->private_data;
  139. seq->private = nn->svc_export_cache;
  140. return 0;
  141. }
  142. static int exports_proc_open(struct inode *inode, struct file *file)
  143. {
  144. return exports_net_open(current->nsproxy->net_ns, file);
  145. }
  146. static const struct proc_ops exports_proc_ops = {
  147. .proc_open = exports_proc_open,
  148. .proc_read = seq_read,
  149. .proc_lseek = seq_lseek,
  150. .proc_release = seq_release,
  151. };
  152. static int exports_nfsd_open(struct inode *inode, struct file *file)
  153. {
  154. return exports_net_open(inode->i_sb->s_fs_info, file);
  155. }
  156. static const struct file_operations exports_nfsd_operations = {
  157. .open = exports_nfsd_open,
  158. .read = seq_read,
  159. .llseek = seq_lseek,
  160. .release = seq_release,
  161. };
  162. static int export_features_show(struct seq_file *m, void *v)
  163. {
  164. seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
  165. return 0;
  166. }
  167. DEFINE_SHOW_ATTRIBUTE(export_features);
  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. DEFINE_SHOW_ATTRIBUTE(supported_enctypes);
  175. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  176. static const struct file_operations pool_stats_operations = {
  177. .open = nfsd_pool_stats_open,
  178. .read = seq_read,
  179. .llseek = seq_lseek,
  180. .release = nfsd_pool_stats_release,
  181. };
  182. DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
  183. DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
  184. /*----------------------------------------------------------------------------*/
  185. /*
  186. * payload - write methods
  187. */
  188. static inline struct net *netns(struct file *file)
  189. {
  190. return file_inode(file)->i_sb->s_fs_info;
  191. }
  192. /*
  193. * write_unlock_ip - Release all locks used by a client
  194. *
  195. * Experimental.
  196. *
  197. * Input:
  198. * buf: '\n'-terminated C string containing a
  199. * presentation format IP address
  200. * size: length of C string in @buf
  201. * Output:
  202. * On success: returns zero if all specified locks were released;
  203. * returns one if one or more locks were not released
  204. * On error: return code is negative errno value
  205. */
  206. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
  207. {
  208. struct sockaddr_storage address;
  209. struct sockaddr *sap = (struct sockaddr *)&address;
  210. size_t salen = sizeof(address);
  211. char *fo_path;
  212. struct net *net = netns(file);
  213. /* sanity check */
  214. if (size == 0)
  215. return -EINVAL;
  216. if (buf[size-1] != '\n')
  217. return -EINVAL;
  218. fo_path = buf;
  219. if (qword_get(&buf, fo_path, size) < 0)
  220. return -EINVAL;
  221. if (rpc_pton(net, fo_path, size, sap, salen) == 0)
  222. return -EINVAL;
  223. return nlmsvc_unlock_all_by_ip(sap);
  224. }
  225. /*
  226. * write_unlock_fs - Release all locks on a local file system
  227. *
  228. * Experimental.
  229. *
  230. * Input:
  231. * buf: '\n'-terminated C string containing the
  232. * absolute pathname of a local file system
  233. * size: length of C string in @buf
  234. * Output:
  235. * On success: returns zero if all specified locks were released;
  236. * returns one if one or more locks were not released
  237. * On error: return code is negative errno value
  238. */
  239. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
  240. {
  241. struct path path;
  242. char *fo_path;
  243. int error;
  244. /* sanity check */
  245. if (size == 0)
  246. return -EINVAL;
  247. if (buf[size-1] != '\n')
  248. return -EINVAL;
  249. fo_path = buf;
  250. if (qword_get(&buf, fo_path, size) < 0)
  251. return -EINVAL;
  252. error = kern_path(fo_path, 0, &path);
  253. if (error)
  254. return error;
  255. /*
  256. * XXX: Needs better sanity checking. Otherwise we could end up
  257. * releasing locks on the wrong file system.
  258. *
  259. * For example:
  260. * 1. Does the path refer to a directory?
  261. * 2. Is that directory a mount point, or
  262. * 3. Is that directory the root of an exported file system?
  263. */
  264. error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
  265. path_put(&path);
  266. return error;
  267. }
  268. /*
  269. * write_filehandle - Get a variable-length NFS file handle by path
  270. *
  271. * On input, the buffer contains a '\n'-terminated C string comprised of
  272. * three alphanumeric words separated by whitespace. The string may
  273. * contain escape sequences.
  274. *
  275. * Input:
  276. * buf:
  277. * domain: client domain name
  278. * path: export pathname
  279. * maxsize: numeric maximum size of
  280. * @buf
  281. * size: length of C string in @buf
  282. * Output:
  283. * On success: passed-in buffer filled with '\n'-terminated C
  284. * string containing a ASCII hex text version
  285. * of the NFS file handle;
  286. * return code is the size in bytes of the string
  287. * On error: return code is negative errno value
  288. */
  289. static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
  290. {
  291. char *dname, *path;
  292. int maxsize;
  293. char *mesg = buf;
  294. int len;
  295. struct auth_domain *dom;
  296. struct knfsd_fh fh;
  297. if (size == 0)
  298. return -EINVAL;
  299. if (buf[size-1] != '\n')
  300. return -EINVAL;
  301. buf[size-1] = 0;
  302. dname = mesg;
  303. len = qword_get(&mesg, dname, size);
  304. if (len <= 0)
  305. return -EINVAL;
  306. path = dname+len+1;
  307. len = qword_get(&mesg, path, size);
  308. if (len <= 0)
  309. return -EINVAL;
  310. len = get_int(&mesg, &maxsize);
  311. if (len)
  312. return len;
  313. if (maxsize < NFS_FHSIZE)
  314. return -EINVAL;
  315. maxsize = min(maxsize, NFS3_FHSIZE);
  316. if (qword_get(&mesg, mesg, size)>0)
  317. return -EINVAL;
  318. /* we have all the words, they are in buf.. */
  319. dom = unix_domain_find(dname);
  320. if (!dom)
  321. return -ENOMEM;
  322. len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
  323. auth_domain_put(dom);
  324. if (len)
  325. return len;
  326. mesg = buf;
  327. len = SIMPLE_TRANSACTION_LIMIT;
  328. qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
  329. mesg[-1] = '\n';
  330. return mesg - buf;
  331. }
  332. /*
  333. * write_threads - Start NFSD, or report the current number of running threads
  334. *
  335. * Input:
  336. * buf: ignored
  337. * size: zero
  338. * Output:
  339. * On success: passed-in buffer filled with '\n'-terminated C
  340. * string numeric value representing the number of
  341. * running NFSD threads;
  342. * return code is the size in bytes of the string
  343. * On error: return code is zero
  344. *
  345. * OR
  346. *
  347. * Input:
  348. * buf: C string containing an unsigned
  349. * integer value representing the
  350. * number of NFSD threads to start
  351. * size: non-zero length of C string in @buf
  352. * Output:
  353. * On success: NFS service is started;
  354. * 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 or a negative errno value
  359. */
  360. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  361. {
  362. char *mesg = buf;
  363. int rv;
  364. struct net *net = netns(file);
  365. if (size > 0) {
  366. int newthreads;
  367. rv = get_int(&mesg, &newthreads);
  368. if (rv)
  369. return rv;
  370. if (newthreads < 0)
  371. return -EINVAL;
  372. rv = nfsd_svc(newthreads, net, file->f_cred);
  373. if (rv < 0)
  374. return rv;
  375. } else
  376. rv = nfsd_nrthreads(net);
  377. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
  378. }
  379. /*
  380. * write_pool_threads - Set or report the current number of threads per pool
  381. *
  382. * Input:
  383. * buf: ignored
  384. * size: zero
  385. *
  386. * OR
  387. *
  388. * Input:
  389. * buf: C string containing whitespace-
  390. * separated unsigned integer values
  391. * representing the number of NFSD
  392. * threads to start in each pool
  393. * size: non-zero length of C string in @buf
  394. * Output:
  395. * On success: passed-in buffer filled with '\n'-terminated C
  396. * string containing integer values representing the
  397. * number of NFSD threads in each pool;
  398. * return code is the size in bytes of the string
  399. * On error: return code is zero or a negative errno value
  400. */
  401. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
  402. {
  403. /* if size > 0, look for an array of number of threads per node
  404. * and apply them then write out number of threads per node as reply
  405. */
  406. char *mesg = buf;
  407. int i;
  408. int rv;
  409. int len;
  410. int npools;
  411. int *nthreads;
  412. struct net *net = netns(file);
  413. mutex_lock(&nfsd_mutex);
  414. npools = nfsd_nrpools(net);
  415. if (npools == 0) {
  416. /*
  417. * NFS is shut down. The admin can start it by
  418. * writing to the threads file but NOT the pool_threads
  419. * file, sorry. Report zero threads.
  420. */
  421. mutex_unlock(&nfsd_mutex);
  422. strcpy(buf, "0\n");
  423. return strlen(buf);
  424. }
  425. nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
  426. rv = -ENOMEM;
  427. if (nthreads == NULL)
  428. goto out_free;
  429. if (size > 0) {
  430. for (i = 0; i < npools; i++) {
  431. rv = get_int(&mesg, &nthreads[i]);
  432. if (rv == -ENOENT)
  433. break; /* fewer numbers than pools */
  434. if (rv)
  435. goto out_free; /* syntax error */
  436. rv = -EINVAL;
  437. if (nthreads[i] < 0)
  438. goto out_free;
  439. }
  440. rv = nfsd_set_nrthreads(i, nthreads, net);
  441. if (rv)
  442. goto out_free;
  443. }
  444. rv = nfsd_get_nrthreads(npools, nthreads, net);
  445. if (rv)
  446. goto out_free;
  447. mesg = buf;
  448. size = SIMPLE_TRANSACTION_LIMIT;
  449. for (i = 0; i < npools && size > 0; i++) {
  450. snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
  451. len = strlen(mesg);
  452. size -= len;
  453. mesg += len;
  454. }
  455. rv = mesg - buf;
  456. out_free:
  457. kfree(nthreads);
  458. mutex_unlock(&nfsd_mutex);
  459. return rv;
  460. }
  461. static ssize_t
  462. nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
  463. const char *sep, unsigned vers, int minor)
  464. {
  465. const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
  466. bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
  467. if (vers == 4 && minor >= 0 &&
  468. !nfsd_minorversion(nn, minor, NFSD_TEST))
  469. supported = false;
  470. if (minor == 0 && supported)
  471. /*
  472. * special case for backward compatability.
  473. * +4.0 is never reported, it is implied by
  474. * +4, unless -4.0 is present.
  475. */
  476. return 0;
  477. return snprintf(buf, remaining, format, sep,
  478. supported ? '+' : '-', vers, minor);
  479. }
  480. static ssize_t __write_versions(struct file *file, char *buf, size_t size)
  481. {
  482. char *mesg = buf;
  483. char *vers, *minorp, sign;
  484. int len, num, remaining;
  485. ssize_t tlen = 0;
  486. char *sep;
  487. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  488. if (size>0) {
  489. if (nn->nfsd_serv)
  490. /* Cannot change versions without updating
  491. * nn->nfsd_serv->sv_xdrsize, and reallocing
  492. * rq_argp and rq_resp
  493. */
  494. return -EBUSY;
  495. if (buf[size-1] != '\n')
  496. return -EINVAL;
  497. buf[size-1] = 0;
  498. vers = mesg;
  499. len = qword_get(&mesg, vers, size);
  500. if (len <= 0) return -EINVAL;
  501. do {
  502. enum vers_op cmd;
  503. unsigned minor;
  504. sign = *vers;
  505. if (sign == '+' || sign == '-')
  506. num = simple_strtol((vers+1), &minorp, 0);
  507. else
  508. num = simple_strtol(vers, &minorp, 0);
  509. if (*minorp == '.') {
  510. if (num != 4)
  511. return -EINVAL;
  512. if (kstrtouint(minorp+1, 0, &minor) < 0)
  513. return -EINVAL;
  514. }
  515. cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
  516. switch(num) {
  517. case 2:
  518. case 3:
  519. nfsd_vers(nn, num, cmd);
  520. break;
  521. case 4:
  522. if (*minorp == '.') {
  523. if (nfsd_minorversion(nn, minor, cmd) < 0)
  524. return -EINVAL;
  525. } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
  526. /*
  527. * Either we have +4 and no minors are enabled,
  528. * or we have -4 and at least one minor is enabled.
  529. * In either case, propagate 'cmd' to all minors.
  530. */
  531. minor = 0;
  532. while (nfsd_minorversion(nn, minor, cmd) >= 0)
  533. minor++;
  534. }
  535. break;
  536. default:
  537. return -EINVAL;
  538. }
  539. vers += len + 1;
  540. } while ((len = qword_get(&mesg, vers, size)) > 0);
  541. /* If all get turned off, turn them back on, as
  542. * having no versions is BAD
  543. */
  544. nfsd_reset_versions(nn);
  545. }
  546. /* Now write current state into reply buffer */
  547. sep = "";
  548. remaining = SIMPLE_TRANSACTION_LIMIT;
  549. for (num=2 ; num <= 4 ; num++) {
  550. int minor;
  551. if (!nfsd_vers(nn, num, NFSD_AVAIL))
  552. continue;
  553. minor = -1;
  554. do {
  555. len = nfsd_print_version_support(nn, buf, remaining,
  556. sep, num, minor);
  557. if (len >= remaining)
  558. goto out;
  559. remaining -= len;
  560. buf += len;
  561. tlen += len;
  562. minor++;
  563. if (len)
  564. sep = " ";
  565. } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
  566. }
  567. out:
  568. len = snprintf(buf, remaining, "\n");
  569. if (len >= remaining)
  570. return -EINVAL;
  571. return tlen + len;
  572. }
  573. /*
  574. * write_versions - Set or report the available NFS protocol versions
  575. *
  576. * Input:
  577. * buf: ignored
  578. * size: zero
  579. * Output:
  580. * On success: passed-in buffer filled with '\n'-terminated C
  581. * string containing positive or negative integer
  582. * values representing the current status of each
  583. * protocol version;
  584. * return code is the size in bytes of the string
  585. * On error: return code is zero or a negative errno value
  586. *
  587. * OR
  588. *
  589. * Input:
  590. * buf: C string containing whitespace-
  591. * separated positive or negative
  592. * integer values representing NFS
  593. * protocol versions to enable ("+n")
  594. * or disable ("-n")
  595. * size: non-zero length of C string in @buf
  596. * Output:
  597. * On success: status of zero or more protocol versions has
  598. * been updated; passed-in buffer filled with
  599. * '\n'-terminated C string containing positive
  600. * or negative integer values representing the
  601. * current status of each protocol version;
  602. * return code is the size in bytes of the string
  603. * On error: return code is zero or a negative errno value
  604. */
  605. static ssize_t write_versions(struct file *file, char *buf, size_t size)
  606. {
  607. ssize_t rv;
  608. mutex_lock(&nfsd_mutex);
  609. rv = __write_versions(file, buf, size);
  610. mutex_unlock(&nfsd_mutex);
  611. return rv;
  612. }
  613. /*
  614. * Zero-length write. Return a list of NFSD's current listener
  615. * transports.
  616. */
  617. static ssize_t __write_ports_names(char *buf, struct net *net)
  618. {
  619. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  620. if (nn->nfsd_serv == NULL)
  621. return 0;
  622. return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
  623. }
  624. /*
  625. * A single 'fd' number was written, in which case it must be for
  626. * a socket of a supported family/protocol, and we use it as an
  627. * nfsd listener.
  628. */
  629. static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
  630. {
  631. char *mesg = buf;
  632. int fd, err;
  633. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  634. err = get_int(&mesg, &fd);
  635. if (err != 0 || fd < 0)
  636. return -EINVAL;
  637. err = nfsd_create_serv(net);
  638. if (err != 0)
  639. return err;
  640. err = svc_addsock(nn->nfsd_serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
  641. if (err >= 0 &&
  642. !nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
  643. svc_get(nn->nfsd_serv);
  644. nfsd_put(net);
  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, const struct cred *cred)
  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_xprt_create(nn->nfsd_serv, transport, net,
  665. PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
  666. if (err < 0)
  667. goto out_err;
  668. err = svc_xprt_create(nn->nfsd_serv, transport, net,
  669. PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
  670. if (err < 0 && err != -EAFNOSUPPORT)
  671. goto out_close;
  672. if (!nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
  673. svc_get(nn->nfsd_serv);
  674. nfsd_put(net);
  675. return 0;
  676. out_close:
  677. xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
  678. if (xprt != NULL) {
  679. svc_xprt_close(xprt);
  680. svc_xprt_put(xprt);
  681. }
  682. out_err:
  683. nfsd_put(net);
  684. return err;
  685. }
  686. static ssize_t __write_ports(struct file *file, char *buf, size_t size,
  687. struct net *net)
  688. {
  689. if (size == 0)
  690. return __write_ports_names(buf, net);
  691. if (isdigit(buf[0]))
  692. return __write_ports_addfd(buf, net, file->f_cred);
  693. if (isalpha(buf[0]))
  694. return __write_ports_addxprt(buf, net, file->f_cred);
  695. return -EINVAL;
  696. }
  697. /*
  698. * write_ports - Pass a socket file descriptor or transport name to listen on
  699. *
  700. * Input:
  701. * buf: ignored
  702. * size: zero
  703. * Output:
  704. * On success: passed-in buffer filled with a '\n'-terminated C
  705. * string containing a whitespace-separated list of
  706. * named NFSD listeners;
  707. * return code is the size in bytes of the string
  708. * On error: return code is zero or a negative errno value
  709. *
  710. * OR
  711. *
  712. * Input:
  713. * buf: C string containing an unsigned
  714. * integer value representing a bound
  715. * but unconnected socket that is to be
  716. * used as an NFSD listener; listen(3)
  717. * must be called for a SOCK_STREAM
  718. * socket, otherwise it is ignored
  719. * size: non-zero length of C string in @buf
  720. * Output:
  721. * On success: NFS service is started;
  722. * passed-in buffer filled with a '\n'-terminated C
  723. * string containing a unique alphanumeric name of
  724. * the listener;
  725. * return code is the size in bytes of the string
  726. * On error: return code is a negative errno value
  727. *
  728. * OR
  729. *
  730. * Input:
  731. * buf: C string containing a transport
  732. * name and an unsigned integer value
  733. * representing the port to listen on,
  734. * separated by whitespace
  735. * size: non-zero length of C string in @buf
  736. * Output:
  737. * On success: returns zero; NFS service is started
  738. * On error: return code is a negative errno value
  739. */
  740. static ssize_t write_ports(struct file *file, char *buf, size_t size)
  741. {
  742. ssize_t rv;
  743. mutex_lock(&nfsd_mutex);
  744. rv = __write_ports(file, buf, size, netns(file));
  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 nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  774. if (size > 0) {
  775. int bsize;
  776. int rv = get_int(&mesg, &bsize);
  777. if (rv)
  778. return rv;
  779. /* force bsize into allowed range and
  780. * required alignment.
  781. */
  782. bsize = max_t(int, bsize, 1024);
  783. bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
  784. bsize &= ~(1024-1);
  785. mutex_lock(&nfsd_mutex);
  786. if (nn->nfsd_serv) {
  787. mutex_unlock(&nfsd_mutex);
  788. return -EBUSY;
  789. }
  790. nfsd_max_blksize = bsize;
  791. mutex_unlock(&nfsd_mutex);
  792. }
  793. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
  794. nfsd_max_blksize);
  795. }
  796. /*
  797. * write_maxconn - Set or report the current max number of connections
  798. *
  799. * Input:
  800. * buf: ignored
  801. * size: zero
  802. * OR
  803. *
  804. * Input:
  805. * buf: C string containing an unsigned
  806. * integer value representing the new
  807. * number of max connections
  808. * size: non-zero length of C string in @buf
  809. * Output:
  810. * On success: passed-in buffer filled with '\n'-terminated C string
  811. * containing numeric value of max_connections setting
  812. * for this net namespace;
  813. * return code is the size in bytes of the string
  814. * On error: return code is zero or a negative errno value
  815. */
  816. static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
  817. {
  818. char *mesg = buf;
  819. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  820. unsigned int maxconn = nn->max_connections;
  821. if (size > 0) {
  822. int rv = get_uint(&mesg, &maxconn);
  823. if (rv)
  824. return rv;
  825. nn->max_connections = maxconn;
  826. }
  827. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
  828. }
  829. #ifdef CONFIG_NFSD_V4
  830. static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
  831. time64_t *time, struct nfsd_net *nn)
  832. {
  833. char *mesg = buf;
  834. int rv, i;
  835. if (size > 0) {
  836. if (nn->nfsd_serv)
  837. return -EBUSY;
  838. rv = get_int(&mesg, &i);
  839. if (rv)
  840. return rv;
  841. /*
  842. * Some sanity checking. We don't have a reason for
  843. * these particular numbers, but problems with the
  844. * extremes are:
  845. * - Too short: the briefest network outage may
  846. * cause clients to lose all their locks. Also,
  847. * the frequent polling may be wasteful.
  848. * - Too long: do you really want reboot recovery
  849. * to take more than an hour? Or to make other
  850. * clients wait an hour before being able to
  851. * revoke a dead client's locks?
  852. */
  853. if (i < 10 || i > 3600)
  854. return -EINVAL;
  855. *time = i;
  856. }
  857. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
  858. }
  859. static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
  860. time64_t *time, struct nfsd_net *nn)
  861. {
  862. ssize_t rv;
  863. mutex_lock(&nfsd_mutex);
  864. rv = __nfsd4_write_time(file, buf, size, time, nn);
  865. mutex_unlock(&nfsd_mutex);
  866. return rv;
  867. }
  868. /*
  869. * write_leasetime - Set or report the current NFSv4 lease time
  870. *
  871. * Input:
  872. * buf: ignored
  873. * size: zero
  874. *
  875. * OR
  876. *
  877. * Input:
  878. * buf: C string containing an unsigned
  879. * integer value representing the new
  880. * NFSv4 lease expiry time
  881. * size: non-zero length of C string in @buf
  882. * Output:
  883. * On success: passed-in buffer filled with '\n'-terminated C
  884. * string containing unsigned integer value of the
  885. * current lease expiry time;
  886. * return code is the size in bytes of the string
  887. * On error: return code is zero or a negative errno value
  888. */
  889. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  890. {
  891. struct nfsd_net *nn = net_generic(netns(file), 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 nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  907. return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
  908. }
  909. static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
  910. struct nfsd_net *nn)
  911. {
  912. char *mesg = buf;
  913. char *recdir;
  914. int len, status;
  915. if (size > 0) {
  916. if (nn->nfsd_serv)
  917. return -EBUSY;
  918. if (size > PATH_MAX || buf[size-1] != '\n')
  919. return -EINVAL;
  920. buf[size-1] = 0;
  921. recdir = mesg;
  922. len = qword_get(&mesg, recdir, size);
  923. if (len <= 0)
  924. return -EINVAL;
  925. status = nfs4_reset_recoverydir(recdir);
  926. if (status)
  927. return status;
  928. }
  929. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
  930. nfs4_recoverydir());
  931. }
  932. /*
  933. * write_recoverydir - Set or report the pathname of the recovery directory
  934. *
  935. * Input:
  936. * buf: ignored
  937. * size: zero
  938. *
  939. * OR
  940. *
  941. * Input:
  942. * buf: C string containing the pathname
  943. * of the directory on a local file
  944. * system containing permanent NFSv4
  945. * recovery data
  946. * size: non-zero length of C string in @buf
  947. * Output:
  948. * On success: passed-in buffer filled with '\n'-terminated C string
  949. * containing the current recovery pathname setting;
  950. * return code is the size in bytes of the string
  951. * On error: return code is zero or a negative errno value
  952. */
  953. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  954. {
  955. ssize_t rv;
  956. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  957. mutex_lock(&nfsd_mutex);
  958. rv = __write_recoverydir(file, buf, size, nn);
  959. mutex_unlock(&nfsd_mutex);
  960. return rv;
  961. }
  962. /*
  963. * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
  964. *
  965. * Input:
  966. * buf: ignored
  967. * size: zero
  968. * OR
  969. *
  970. * Input:
  971. * buf: any value
  972. * size: non-zero length of C string in @buf
  973. * Output:
  974. * passed-in buffer filled with "Y" or "N" with a newline
  975. * and NULL-terminated C string. This indicates whether
  976. * the grace period has ended in the current net
  977. * namespace. Return code is the size in bytes of the
  978. * string. Writing a string that starts with 'Y', 'y', or
  979. * '1' to the file will end the grace period for nfsd's v4
  980. * lock manager.
  981. */
  982. static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
  983. {
  984. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  985. if (size > 0) {
  986. switch(buf[0]) {
  987. case 'Y':
  988. case 'y':
  989. case '1':
  990. if (!nn->nfsd_serv)
  991. return -EBUSY;
  992. nfsd4_end_grace(nn);
  993. break;
  994. default:
  995. return -EINVAL;
  996. }
  997. }
  998. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
  999. nn->grace_ended ? 'Y' : 'N');
  1000. }
  1001. #endif
  1002. /*----------------------------------------------------------------------------*/
  1003. /*
  1004. * populating the filesystem.
  1005. */
  1006. /* Basically copying rpc_get_inode. */
  1007. static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
  1008. {
  1009. struct inode *inode = new_inode(sb);
  1010. if (!inode)
  1011. return NULL;
  1012. /* Following advice from simple_fill_super documentation: */
  1013. inode->i_ino = iunique(sb, NFSD_MaxReserved);
  1014. inode->i_mode = mode;
  1015. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  1016. switch (mode & S_IFMT) {
  1017. case S_IFDIR:
  1018. inode->i_fop = &simple_dir_operations;
  1019. inode->i_op = &simple_dir_inode_operations;
  1020. inc_nlink(inode);
  1021. break;
  1022. default:
  1023. break;
  1024. }
  1025. return inode;
  1026. }
  1027. static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
  1028. {
  1029. struct inode *inode;
  1030. inode = nfsd_get_inode(dir->i_sb, mode);
  1031. if (!inode)
  1032. return -ENOMEM;
  1033. if (ncl) {
  1034. inode->i_private = ncl;
  1035. kref_get(&ncl->cl_ref);
  1036. }
  1037. d_add(dentry, inode);
  1038. inc_nlink(dir);
  1039. fsnotify_mkdir(dir, dentry);
  1040. return 0;
  1041. }
  1042. static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
  1043. {
  1044. struct inode *dir = parent->d_inode;
  1045. struct dentry *dentry;
  1046. int ret = -ENOMEM;
  1047. inode_lock(dir);
  1048. dentry = d_alloc_name(parent, name);
  1049. if (!dentry)
  1050. goto out_err;
  1051. ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
  1052. if (ret)
  1053. goto out_err;
  1054. out:
  1055. inode_unlock(dir);
  1056. return dentry;
  1057. out_err:
  1058. dput(dentry);
  1059. dentry = ERR_PTR(ret);
  1060. goto out;
  1061. }
  1062. static void clear_ncl(struct inode *inode)
  1063. {
  1064. struct nfsdfs_client *ncl = inode->i_private;
  1065. inode->i_private = NULL;
  1066. kref_put(&ncl->cl_ref, ncl->cl_release);
  1067. }
  1068. static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
  1069. {
  1070. struct nfsdfs_client *nc = inode->i_private;
  1071. if (nc)
  1072. kref_get(&nc->cl_ref);
  1073. return nc;
  1074. }
  1075. struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
  1076. {
  1077. struct nfsdfs_client *nc;
  1078. inode_lock_shared(inode);
  1079. nc = __get_nfsdfs_client(inode);
  1080. inode_unlock_shared(inode);
  1081. return nc;
  1082. }
  1083. /* from __rpc_unlink */
  1084. static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
  1085. {
  1086. int ret;
  1087. clear_ncl(d_inode(dentry));
  1088. dget(dentry);
  1089. ret = simple_unlink(dir, dentry);
  1090. d_drop(dentry);
  1091. fsnotify_unlink(dir, dentry);
  1092. dput(dentry);
  1093. WARN_ON_ONCE(ret);
  1094. }
  1095. static void nfsdfs_remove_files(struct dentry *root)
  1096. {
  1097. struct dentry *dentry, *tmp;
  1098. list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
  1099. if (!simple_positive(dentry)) {
  1100. WARN_ON_ONCE(1); /* I think this can't happen? */
  1101. continue;
  1102. }
  1103. nfsdfs_remove_file(d_inode(root), dentry);
  1104. }
  1105. }
  1106. /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
  1107. * code instead. */
  1108. static int nfsdfs_create_files(struct dentry *root,
  1109. const struct tree_descr *files,
  1110. struct dentry **fdentries)
  1111. {
  1112. struct inode *dir = d_inode(root);
  1113. struct inode *inode;
  1114. struct dentry *dentry;
  1115. int i;
  1116. inode_lock(dir);
  1117. for (i = 0; files->name && files->name[0]; i++, files++) {
  1118. dentry = d_alloc_name(root, files->name);
  1119. if (!dentry)
  1120. goto out;
  1121. inode = nfsd_get_inode(d_inode(root)->i_sb,
  1122. S_IFREG | files->mode);
  1123. if (!inode) {
  1124. dput(dentry);
  1125. goto out;
  1126. }
  1127. inode->i_fop = files->ops;
  1128. inode->i_private = __get_nfsdfs_client(dir);
  1129. d_add(dentry, inode);
  1130. fsnotify_create(dir, dentry);
  1131. if (fdentries)
  1132. fdentries[i] = dentry;
  1133. }
  1134. inode_unlock(dir);
  1135. return 0;
  1136. out:
  1137. nfsdfs_remove_files(root);
  1138. inode_unlock(dir);
  1139. return -ENOMEM;
  1140. }
  1141. /* on success, returns positive number unique to that client. */
  1142. struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
  1143. struct nfsdfs_client *ncl, u32 id,
  1144. const struct tree_descr *files,
  1145. struct dentry **fdentries)
  1146. {
  1147. struct dentry *dentry;
  1148. char name[11];
  1149. int ret;
  1150. sprintf(name, "%u", id);
  1151. dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
  1152. if (IS_ERR(dentry)) /* XXX: tossing errors? */
  1153. return NULL;
  1154. ret = nfsdfs_create_files(dentry, files, fdentries);
  1155. if (ret) {
  1156. nfsd_client_rmdir(dentry);
  1157. return NULL;
  1158. }
  1159. return dentry;
  1160. }
  1161. /* Taken from __rpc_rmdir: */
  1162. void nfsd_client_rmdir(struct dentry *dentry)
  1163. {
  1164. struct inode *dir = d_inode(dentry->d_parent);
  1165. struct inode *inode = d_inode(dentry);
  1166. int ret;
  1167. inode_lock(dir);
  1168. nfsdfs_remove_files(dentry);
  1169. clear_ncl(inode);
  1170. dget(dentry);
  1171. ret = simple_rmdir(dir, dentry);
  1172. WARN_ON_ONCE(ret);
  1173. d_drop(dentry);
  1174. fsnotify_rmdir(dir, dentry);
  1175. dput(dentry);
  1176. inode_unlock(dir);
  1177. }
  1178. static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
  1179. {
  1180. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  1181. nfsd_net_id);
  1182. struct dentry *dentry;
  1183. int ret;
  1184. static const struct tree_descr nfsd_files[] = {
  1185. [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
  1186. /* Per-export io stats use same ops as exports file */
  1187. [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
  1188. [NFSD_Export_features] = {"export_features",
  1189. &export_features_fops, S_IRUGO},
  1190. [NFSD_FO_UnlockIP] = {"unlock_ip",
  1191. &transaction_ops, S_IWUSR|S_IRUSR},
  1192. [NFSD_FO_UnlockFS] = {"unlock_filesystem",
  1193. &transaction_ops, S_IWUSR|S_IRUSR},
  1194. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  1195. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  1196. [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
  1197. [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
  1198. [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
  1199. &nfsd_reply_cache_stats_fops, S_IRUGO},
  1200. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  1201. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  1202. [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
  1203. [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
  1204. [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
  1205. #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
  1206. [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes",
  1207. &supported_enctypes_fops, S_IRUGO},
  1208. #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
  1209. #ifdef CONFIG_NFSD_V4
  1210. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  1211. [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
  1212. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  1213. [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
  1214. #endif
  1215. /* last one */ {""}
  1216. };
  1217. ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
  1218. if (ret)
  1219. return ret;
  1220. dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
  1221. if (IS_ERR(dentry))
  1222. return PTR_ERR(dentry);
  1223. nn->nfsd_client_dir = dentry;
  1224. return 0;
  1225. }
  1226. static int nfsd_fs_get_tree(struct fs_context *fc)
  1227. {
  1228. return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
  1229. }
  1230. static void nfsd_fs_free_fc(struct fs_context *fc)
  1231. {
  1232. if (fc->s_fs_info)
  1233. put_net(fc->s_fs_info);
  1234. }
  1235. static const struct fs_context_operations nfsd_fs_context_ops = {
  1236. .free = nfsd_fs_free_fc,
  1237. .get_tree = nfsd_fs_get_tree,
  1238. };
  1239. static int nfsd_init_fs_context(struct fs_context *fc)
  1240. {
  1241. put_user_ns(fc->user_ns);
  1242. fc->user_ns = get_user_ns(fc->net_ns->user_ns);
  1243. fc->ops = &nfsd_fs_context_ops;
  1244. return 0;
  1245. }
  1246. static void nfsd_umount(struct super_block *sb)
  1247. {
  1248. struct net *net = sb->s_fs_info;
  1249. nfsd_shutdown_threads(net);
  1250. kill_litter_super(sb);
  1251. put_net(net);
  1252. }
  1253. static struct file_system_type nfsd_fs_type = {
  1254. .owner = THIS_MODULE,
  1255. .name = "nfsd",
  1256. .init_fs_context = nfsd_init_fs_context,
  1257. .kill_sb = nfsd_umount,
  1258. };
  1259. MODULE_ALIAS_FS("nfsd");
  1260. #ifdef CONFIG_PROC_FS
  1261. static int create_proc_exports_entry(void)
  1262. {
  1263. struct proc_dir_entry *entry;
  1264. entry = proc_mkdir("fs/nfs", NULL);
  1265. if (!entry)
  1266. return -ENOMEM;
  1267. entry = proc_create("exports", 0, entry, &exports_proc_ops);
  1268. if (!entry) {
  1269. remove_proc_entry("fs/nfs", NULL);
  1270. return -ENOMEM;
  1271. }
  1272. return 0;
  1273. }
  1274. #else /* CONFIG_PROC_FS */
  1275. static int create_proc_exports_entry(void)
  1276. {
  1277. return 0;
  1278. }
  1279. #endif
  1280. unsigned int nfsd_net_id;
  1281. static __net_init int nfsd_init_net(struct net *net)
  1282. {
  1283. int retval;
  1284. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1285. retval = nfsd_export_init(net);
  1286. if (retval)
  1287. goto out_export_error;
  1288. retval = nfsd_idmap_init(net);
  1289. if (retval)
  1290. goto out_idmap_error;
  1291. nn->nfsd_versions = NULL;
  1292. nn->nfsd4_minorversions = NULL;
  1293. retval = nfsd4_init_leases_net(nn);
  1294. if (retval)
  1295. goto out_drc_error;
  1296. retval = nfsd_reply_cache_init(nn);
  1297. if (retval)
  1298. goto out_cache_error;
  1299. get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
  1300. seqlock_init(&nn->writeverf_lock);
  1301. return 0;
  1302. out_cache_error:
  1303. nfsd4_leases_net_shutdown(nn);
  1304. out_drc_error:
  1305. nfsd_idmap_shutdown(net);
  1306. out_idmap_error:
  1307. nfsd_export_shutdown(net);
  1308. out_export_error:
  1309. return retval;
  1310. }
  1311. static __net_exit void nfsd_exit_net(struct net *net)
  1312. {
  1313. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1314. nfsd_reply_cache_shutdown(nn);
  1315. nfsd_idmap_shutdown(net);
  1316. nfsd_export_shutdown(net);
  1317. nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
  1318. nfsd4_leases_net_shutdown(nn);
  1319. }
  1320. static struct pernet_operations nfsd_net_ops = {
  1321. .init = nfsd_init_net,
  1322. .exit = nfsd_exit_net,
  1323. .id = &nfsd_net_id,
  1324. .size = sizeof(struct nfsd_net),
  1325. };
  1326. static int __init init_nfsd(void)
  1327. {
  1328. int retval;
  1329. retval = nfsd4_init_slabs();
  1330. if (retval)
  1331. return retval;
  1332. retval = nfsd4_init_pnfs();
  1333. if (retval)
  1334. goto out_free_slabs;
  1335. retval = nfsd_stat_init(); /* Statistics */
  1336. if (retval)
  1337. goto out_free_pnfs;
  1338. retval = nfsd_drc_slab_create();
  1339. if (retval)
  1340. goto out_free_stat;
  1341. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  1342. retval = create_proc_exports_entry();
  1343. if (retval)
  1344. goto out_free_lockd;
  1345. retval = register_pernet_subsys(&nfsd_net_ops);
  1346. if (retval < 0)
  1347. goto out_free_exports;
  1348. retval = register_cld_notifier();
  1349. if (retval)
  1350. goto out_free_subsys;
  1351. retval = nfsd4_create_laundry_wq();
  1352. if (retval)
  1353. goto out_free_cld;
  1354. retval = register_filesystem(&nfsd_fs_type);
  1355. if (retval)
  1356. goto out_free_all;
  1357. return 0;
  1358. out_free_all:
  1359. nfsd4_destroy_laundry_wq();
  1360. out_free_cld:
  1361. unregister_cld_notifier();
  1362. out_free_subsys:
  1363. unregister_pernet_subsys(&nfsd_net_ops);
  1364. out_free_exports:
  1365. remove_proc_entry("fs/nfs/exports", NULL);
  1366. remove_proc_entry("fs/nfs", NULL);
  1367. out_free_lockd:
  1368. nfsd_lockd_shutdown();
  1369. nfsd_drc_slab_free();
  1370. out_free_stat:
  1371. nfsd_stat_shutdown();
  1372. out_free_pnfs:
  1373. nfsd4_exit_pnfs();
  1374. out_free_slabs:
  1375. nfsd4_free_slabs();
  1376. return retval;
  1377. }
  1378. static void __exit exit_nfsd(void)
  1379. {
  1380. unregister_filesystem(&nfsd_fs_type);
  1381. nfsd4_destroy_laundry_wq();
  1382. unregister_cld_notifier();
  1383. unregister_pernet_subsys(&nfsd_net_ops);
  1384. nfsd_drc_slab_free();
  1385. remove_proc_entry("fs/nfs/exports", NULL);
  1386. remove_proc_entry("fs/nfs", NULL);
  1387. nfsd_stat_shutdown();
  1388. nfsd_lockd_shutdown();
  1389. nfsd4_free_slabs();
  1390. nfsd4_exit_pnfs();
  1391. }
  1392. MODULE_AUTHOR("Olaf Kirch <[email protected]>");
  1393. MODULE_LICENSE("GPL");
  1394. module_init(init_nfsd)
  1395. module_exit(exit_nfsd)