nfsctl.c 39 KB

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