nfsctl.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  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/rpc_pipe_fs.h>
  16. #include <linux/sunrpc/svc.h>
  17. #include <linux/module.h>
  18. #include <linux/fsnotify.h>
  19. #include <linux/nfslocalio.h>
  20. #include "idmap.h"
  21. #include "nfsd.h"
  22. #include "cache.h"
  23. #include "state.h"
  24. #include "netns.h"
  25. #include "pnfs.h"
  26. #include "filecache.h"
  27. #include "trace.h"
  28. #include "netlink.h"
  29. /*
  30. * We have a single directory with several nodes in it.
  31. */
  32. enum {
  33. NFSD_Root = 1,
  34. NFSD_List,
  35. NFSD_Export_Stats,
  36. NFSD_Export_features,
  37. NFSD_Fh,
  38. NFSD_FO_UnlockIP,
  39. NFSD_FO_UnlockFS,
  40. NFSD_Threads,
  41. NFSD_Pool_Threads,
  42. NFSD_Pool_Stats,
  43. NFSD_Reply_Cache_Stats,
  44. NFSD_Versions,
  45. NFSD_Ports,
  46. NFSD_MaxBlkSize,
  47. NFSD_Filecache,
  48. NFSD_Leasetime,
  49. NFSD_Gracetime,
  50. NFSD_RecoveryDir,
  51. NFSD_V4EndGrace,
  52. NFSD_MaxReserved
  53. };
  54. /*
  55. * write() for these nodes.
  56. */
  57. static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
  58. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
  59. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
  60. static ssize_t write_threads(struct file *file, char *buf, size_t size);
  61. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
  62. static ssize_t write_versions(struct file *file, char *buf, size_t size);
  63. static ssize_t write_ports(struct file *file, char *buf, size_t size);
  64. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
  65. #ifdef CONFIG_NFSD_V4
  66. static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
  67. static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
  68. #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
  69. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
  70. #endif
  71. static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
  72. #endif
  73. static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
  74. [NFSD_Fh] = write_filehandle,
  75. [NFSD_FO_UnlockIP] = write_unlock_ip,
  76. [NFSD_FO_UnlockFS] = write_unlock_fs,
  77. [NFSD_Threads] = write_threads,
  78. [NFSD_Pool_Threads] = write_pool_threads,
  79. [NFSD_Versions] = write_versions,
  80. [NFSD_Ports] = write_ports,
  81. [NFSD_MaxBlkSize] = write_maxblksize,
  82. #ifdef CONFIG_NFSD_V4
  83. [NFSD_Leasetime] = write_leasetime,
  84. [NFSD_Gracetime] = write_gracetime,
  85. #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
  86. [NFSD_RecoveryDir] = write_recoverydir,
  87. #endif
  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. return rv;
  104. simple_transaction_set(file, rv);
  105. return size;
  106. }
  107. static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  108. {
  109. if (! file->private_data) {
  110. /* An attempt to read a transaction file without writing
  111. * causes a 0-byte write so that the file can return
  112. * state information
  113. */
  114. ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
  115. if (rv < 0)
  116. return rv;
  117. }
  118. return simple_transaction_read(file, buf, size, pos);
  119. }
  120. static const struct file_operations transaction_ops = {
  121. .write = nfsctl_transaction_write,
  122. .read = nfsctl_transaction_read,
  123. .release = simple_transaction_release,
  124. .llseek = default_llseek,
  125. };
  126. static int exports_net_open(struct net *net, struct file *file)
  127. {
  128. int err;
  129. struct seq_file *seq;
  130. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  131. err = seq_open(file, &nfs_exports_op);
  132. if (err)
  133. return err;
  134. seq = file->private_data;
  135. seq->private = nn->svc_export_cache;
  136. get_net(net);
  137. return 0;
  138. }
  139. static int exports_release(struct inode *inode, struct file *file)
  140. {
  141. struct seq_file *seq = file->private_data;
  142. struct cache_detail *cd = seq->private;
  143. put_net(cd->net);
  144. return seq_release(inode, file);
  145. }
  146. static int exports_nfsd_open(struct inode *inode, struct file *file)
  147. {
  148. return exports_net_open(inode->i_sb->s_fs_info, file);
  149. }
  150. static const struct file_operations exports_nfsd_operations = {
  151. .open = exports_nfsd_open,
  152. .read = seq_read,
  153. .llseek = seq_lseek,
  154. .release = exports_release,
  155. };
  156. static int export_features_show(struct seq_file *m, void *v)
  157. {
  158. seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
  159. return 0;
  160. }
  161. DEFINE_SHOW_ATTRIBUTE(export_features);
  162. static int nfsd_pool_stats_open(struct inode *inode, struct file *file)
  163. {
  164. struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
  165. return svc_pool_stats_open(&nn->nfsd_info, file);
  166. }
  167. static const struct file_operations pool_stats_operations = {
  168. .open = nfsd_pool_stats_open,
  169. .read = seq_read,
  170. .llseek = seq_lseek,
  171. .release = seq_release,
  172. };
  173. DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
  174. DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
  175. /*----------------------------------------------------------------------------*/
  176. /*
  177. * payload - write methods
  178. */
  179. static inline struct net *netns(struct file *file)
  180. {
  181. return file_inode(file)->i_sb->s_fs_info;
  182. }
  183. /*
  184. * write_unlock_ip - Release all locks used by a client
  185. *
  186. * Experimental.
  187. *
  188. * Input:
  189. * buf: '\n'-terminated C string containing a
  190. * presentation format IP address
  191. * size: length of C string in @buf
  192. * Output:
  193. * On success: returns zero if all specified locks were released;
  194. * returns one if one or more locks were not released
  195. * On error: return code is negative errno value
  196. */
  197. static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
  198. {
  199. struct sockaddr_storage address;
  200. struct sockaddr *sap = (struct sockaddr *)&address;
  201. size_t salen = sizeof(address);
  202. char *fo_path;
  203. struct net *net = netns(file);
  204. /* sanity check */
  205. if (size == 0)
  206. return -EINVAL;
  207. if (buf[size-1] != '\n')
  208. return -EINVAL;
  209. fo_path = buf;
  210. if (qword_get(&buf, fo_path, size) < 0)
  211. return -EINVAL;
  212. if (rpc_pton(net, fo_path, size, sap, salen) == 0)
  213. return -EINVAL;
  214. trace_nfsd_ctl_unlock_ip(net, buf);
  215. return nlmsvc_unlock_all_by_ip(sap);
  216. }
  217. /*
  218. * write_unlock_fs - Release all locks on a local file system
  219. *
  220. * Experimental.
  221. *
  222. * Input:
  223. * buf: '\n'-terminated C string containing the
  224. * absolute pathname of a local file system
  225. * size: length of C string in @buf
  226. * Output:
  227. * On success: returns zero if all specified locks were released;
  228. * returns one if one or more locks were not released
  229. * On error: return code is negative errno value
  230. */
  231. static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
  232. {
  233. struct path path;
  234. char *fo_path;
  235. int error;
  236. struct nfsd_net *nn;
  237. /* sanity check */
  238. if (size == 0)
  239. return -EINVAL;
  240. if (buf[size-1] != '\n')
  241. return -EINVAL;
  242. fo_path = buf;
  243. if (qword_get(&buf, fo_path, size) < 0)
  244. return -EINVAL;
  245. trace_nfsd_ctl_unlock_fs(netns(file), fo_path);
  246. error = kern_path(fo_path, 0, &path);
  247. if (error)
  248. return error;
  249. /*
  250. * XXX: Needs better sanity checking. Otherwise we could end up
  251. * releasing locks on the wrong file system.
  252. *
  253. * For example:
  254. * 1. Does the path refer to a directory?
  255. * 2. Is that directory a mount point, or
  256. * 3. Is that directory the root of an exported file system?
  257. */
  258. nfsd4_cancel_copy_by_sb(netns(file), path.dentry->d_sb);
  259. error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
  260. mutex_lock(&nfsd_mutex);
  261. nn = net_generic(netns(file), nfsd_net_id);
  262. if (nn->nfsd_serv)
  263. nfsd4_revoke_states(nn, path.dentry->d_sb);
  264. else
  265. error = -EINVAL;
  266. mutex_unlock(&nfsd_mutex);
  267. path_put(&path);
  268. return error;
  269. }
  270. /*
  271. * write_filehandle - Get a variable-length NFS file handle by path
  272. *
  273. * On input, the buffer contains a '\n'-terminated C string comprised of
  274. * three alphanumeric words separated by whitespace. The string may
  275. * contain escape sequences.
  276. *
  277. * Input:
  278. * buf:
  279. * domain: client domain name
  280. * path: export pathname
  281. * maxsize: numeric maximum size of
  282. * @buf
  283. * size: length of C string in @buf
  284. * Output:
  285. * On success: passed-in buffer filled with '\n'-terminated C
  286. * string containing a ASCII hex text version
  287. * of the NFS file handle;
  288. * return code is the size in bytes of the string
  289. * On error: return code is negative errno value
  290. */
  291. static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
  292. {
  293. char *dname, *path;
  294. int maxsize;
  295. char *mesg = buf;
  296. int len;
  297. struct auth_domain *dom;
  298. struct knfsd_fh fh;
  299. if (size == 0)
  300. return -EINVAL;
  301. if (buf[size-1] != '\n')
  302. return -EINVAL;
  303. buf[size-1] = 0;
  304. dname = mesg;
  305. len = qword_get(&mesg, dname, size);
  306. if (len <= 0)
  307. return -EINVAL;
  308. path = dname+len+1;
  309. len = qword_get(&mesg, path, size);
  310. if (len <= 0)
  311. return -EINVAL;
  312. len = get_int(&mesg, &maxsize);
  313. if (len)
  314. return len;
  315. if (maxsize < NFS_FHSIZE)
  316. return -EINVAL;
  317. maxsize = min(maxsize, NFS3_FHSIZE);
  318. if (qword_get(&mesg, mesg, size) > 0)
  319. return -EINVAL;
  320. trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize);
  321. /* we have all the words, they are in buf.. */
  322. dom = unix_domain_find(dname);
  323. if (!dom)
  324. return -ENOMEM;
  325. len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
  326. auth_domain_put(dom);
  327. if (len)
  328. return len;
  329. mesg = buf;
  330. len = SIMPLE_TRANSACTION_LIMIT;
  331. qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
  332. mesg[-1] = '\n';
  333. return mesg - buf;
  334. }
  335. /*
  336. * write_threads - Start NFSD, or report the configured number of threads
  337. *
  338. * Input:
  339. * buf: ignored
  340. * size: zero
  341. * Output:
  342. * On success: passed-in buffer filled with '\n'-terminated C
  343. * string numeric value representing the configured
  344. * number of NFSD threads;
  345. * return code is the size in bytes of the string
  346. * On error: return code is zero
  347. *
  348. * OR
  349. *
  350. * Input:
  351. * buf: C string containing an unsigned
  352. * integer value representing the
  353. * number of NFSD threads to start
  354. * size: non-zero length of C string in @buf
  355. * Output:
  356. * On success: NFS service is started;
  357. * passed-in buffer filled with '\n'-terminated C
  358. * string numeric value representing the configured
  359. * number of NFSD threads;
  360. * return code is the size in bytes of the string
  361. * On error: return code is zero or a negative errno value
  362. */
  363. static ssize_t write_threads(struct file *file, char *buf, size_t size)
  364. {
  365. char *mesg = buf;
  366. int rv;
  367. struct net *net = netns(file);
  368. if (size > 0) {
  369. int newthreads;
  370. rv = get_int(&mesg, &newthreads);
  371. if (rv)
  372. return rv;
  373. if (newthreads < 0)
  374. return -EINVAL;
  375. trace_nfsd_ctl_threads(net, newthreads);
  376. mutex_lock(&nfsd_mutex);
  377. rv = nfsd_svc(1, &newthreads, net, file->f_cred, NULL);
  378. mutex_unlock(&nfsd_mutex);
  379. if (rv < 0)
  380. return rv;
  381. } else
  382. rv = nfsd_nrthreads(net);
  383. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
  384. }
  385. /*
  386. * write_pool_threads - Set or report the configured number of threads per pool
  387. *
  388. * Input:
  389. * buf: ignored
  390. * size: zero
  391. *
  392. * OR
  393. *
  394. * Input:
  395. * buf: C string containing whitespace-
  396. * separated unsigned integer values
  397. * representing the number of NFSD
  398. * threads to start in each pool
  399. * size: non-zero length of C string in @buf
  400. * Output:
  401. * On success: passed-in buffer filled with '\n'-terminated C
  402. * string containing integer values representing the
  403. * configured number of NFSD threads in each pool;
  404. * return code is the size in bytes of the string
  405. * On error: return code is zero or a negative errno value
  406. */
  407. static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
  408. {
  409. /* if size > 0, look for an array of number of threads per node
  410. * and apply them then write out number of threads per node as reply
  411. */
  412. char *mesg = buf;
  413. int i;
  414. int rv;
  415. int len;
  416. int npools;
  417. int *nthreads;
  418. struct net *net = netns(file);
  419. mutex_lock(&nfsd_mutex);
  420. npools = nfsd_nrpools(net);
  421. if (npools == 0) {
  422. /*
  423. * NFS is shut down. The admin can start it by
  424. * writing to the threads file but NOT the pool_threads
  425. * file, sorry. Report zero threads.
  426. */
  427. mutex_unlock(&nfsd_mutex);
  428. strcpy(buf, "0\n");
  429. return strlen(buf);
  430. }
  431. nthreads = kzalloc_objs(int, npools);
  432. rv = -ENOMEM;
  433. if (nthreads == NULL)
  434. goto out_free;
  435. if (size > 0) {
  436. for (i = 0; i < npools; i++) {
  437. rv = get_int(&mesg, &nthreads[i]);
  438. if (rv == -ENOENT)
  439. break; /* fewer numbers than pools */
  440. if (rv)
  441. goto out_free; /* syntax error */
  442. rv = -EINVAL;
  443. if (nthreads[i] < 0)
  444. goto out_free;
  445. trace_nfsd_ctl_pool_threads(net, i, nthreads[i]);
  446. }
  447. /*
  448. * There must always be a thread in pool 0; the admin
  449. * can't shut down NFS completely using pool_threads.
  450. */
  451. if (nthreads[0] == 0)
  452. nthreads[0] = 1;
  453. rv = nfsd_set_nrthreads(i, nthreads, net);
  454. if (rv)
  455. goto out_free;
  456. }
  457. rv = nfsd_get_nrthreads(npools, nthreads, net);
  458. if (rv)
  459. goto out_free;
  460. mesg = buf;
  461. size = SIMPLE_TRANSACTION_LIMIT;
  462. for (i = 0; i < npools && size > 0; i++) {
  463. snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
  464. len = strlen(mesg);
  465. size -= len;
  466. mesg += len;
  467. }
  468. rv = mesg - buf;
  469. out_free:
  470. kfree(nthreads);
  471. mutex_unlock(&nfsd_mutex);
  472. return rv;
  473. }
  474. static ssize_t
  475. nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
  476. const char *sep, unsigned vers, int minor)
  477. {
  478. const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
  479. bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
  480. if (vers == 4 && minor >= 0 &&
  481. !nfsd_minorversion(nn, minor, NFSD_TEST))
  482. supported = false;
  483. if (minor == 0 && supported)
  484. /*
  485. * special case for backward compatability.
  486. * +4.0 is never reported, it is implied by
  487. * +4, unless -4.0 is present.
  488. */
  489. return 0;
  490. return snprintf(buf, remaining, format, sep,
  491. supported ? '+' : '-', vers, minor);
  492. }
  493. static ssize_t __write_versions(struct file *file, char *buf, size_t size)
  494. {
  495. char *mesg = buf;
  496. char *vers, *minorp, sign;
  497. int len, num, remaining;
  498. ssize_t tlen = 0;
  499. char *sep;
  500. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  501. if (size > 0) {
  502. if (nn->nfsd_serv)
  503. /* Cannot change versions without updating
  504. * nn->nfsd_serv->sv_xdrsize, and reallocing
  505. * rq_argp and rq_resp
  506. */
  507. return -EBUSY;
  508. if (buf[size-1] != '\n')
  509. return -EINVAL;
  510. buf[size-1] = 0;
  511. trace_nfsd_ctl_version(netns(file), buf);
  512. vers = mesg;
  513. len = qword_get(&mesg, vers, size);
  514. if (len <= 0) return -EINVAL;
  515. do {
  516. enum vers_op cmd;
  517. unsigned minor;
  518. sign = *vers;
  519. if (sign == '+' || sign == '-')
  520. num = simple_strtol((vers+1), &minorp, 0);
  521. else
  522. num = simple_strtol(vers, &minorp, 0);
  523. if (*minorp == '.') {
  524. if (num != 4)
  525. return -EINVAL;
  526. if (kstrtouint(minorp+1, 0, &minor) < 0)
  527. return -EINVAL;
  528. }
  529. cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
  530. switch(num) {
  531. #ifdef CONFIG_NFSD_V2
  532. case 2:
  533. #endif
  534. case 3:
  535. nfsd_vers(nn, num, cmd);
  536. break;
  537. case 4:
  538. if (*minorp == '.') {
  539. if (nfsd_minorversion(nn, minor, cmd) < 0)
  540. return -EINVAL;
  541. } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
  542. /*
  543. * Either we have +4 and no minors are enabled,
  544. * or we have -4 and at least one minor is enabled.
  545. * In either case, propagate 'cmd' to all minors.
  546. */
  547. minor = 0;
  548. while (nfsd_minorversion(nn, minor, cmd) >= 0)
  549. minor++;
  550. }
  551. break;
  552. default:
  553. /* Ignore requests to disable non-existent versions */
  554. if (cmd == NFSD_SET)
  555. return -EINVAL;
  556. }
  557. vers += len + 1;
  558. } while ((len = qword_get(&mesg, vers, size)) > 0);
  559. /* If all get turned off, turn them back on, as
  560. * having no versions is BAD
  561. */
  562. nfsd_reset_versions(nn);
  563. }
  564. /* Now write current state into reply buffer */
  565. sep = "";
  566. remaining = SIMPLE_TRANSACTION_LIMIT;
  567. for (num=2 ; num <= 4 ; num++) {
  568. int minor;
  569. if (!nfsd_vers(nn, num, NFSD_AVAIL))
  570. continue;
  571. minor = -1;
  572. do {
  573. len = nfsd_print_version_support(nn, buf, remaining,
  574. sep, num, minor);
  575. if (len >= remaining)
  576. goto out;
  577. remaining -= len;
  578. buf += len;
  579. tlen += len;
  580. minor++;
  581. if (len)
  582. sep = " ";
  583. } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
  584. }
  585. out:
  586. len = snprintf(buf, remaining, "\n");
  587. if (len >= remaining)
  588. return -EINVAL;
  589. return tlen + len;
  590. }
  591. /*
  592. * write_versions - Set or report the available NFS protocol versions
  593. *
  594. * Input:
  595. * buf: ignored
  596. * size: zero
  597. * Output:
  598. * On success: passed-in buffer filled with '\n'-terminated C
  599. * string containing positive or negative integer
  600. * values representing the current status of each
  601. * 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. * OR
  606. *
  607. * Input:
  608. * buf: C string containing whitespace-
  609. * separated positive or negative
  610. * integer values representing NFS
  611. * protocol versions to enable ("+n")
  612. * or disable ("-n")
  613. * size: non-zero length of C string in @buf
  614. * Output:
  615. * On success: status of zero or more protocol versions has
  616. * been updated; passed-in buffer filled with
  617. * '\n'-terminated C string containing positive
  618. * or negative integer values representing the
  619. * current status of each protocol version;
  620. * return code is the size in bytes of the string
  621. * On error: return code is zero or a negative errno value
  622. */
  623. static ssize_t write_versions(struct file *file, char *buf, size_t size)
  624. {
  625. ssize_t rv;
  626. mutex_lock(&nfsd_mutex);
  627. rv = __write_versions(file, buf, size);
  628. mutex_unlock(&nfsd_mutex);
  629. return rv;
  630. }
  631. /*
  632. * Zero-length write. Return a list of NFSD's current listener
  633. * transports.
  634. */
  635. static ssize_t __write_ports_names(char *buf, struct net *net)
  636. {
  637. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  638. if (nn->nfsd_serv == NULL)
  639. return 0;
  640. return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
  641. }
  642. /*
  643. * A single 'fd' number was written, in which case it must be for
  644. * a socket of a supported family/protocol, and we use it as an
  645. * nfsd listener.
  646. */
  647. static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
  648. {
  649. char *mesg = buf;
  650. int fd, err;
  651. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  652. struct svc_serv *serv;
  653. err = get_int(&mesg, &fd);
  654. if (err != 0 || fd < 0)
  655. return -EINVAL;
  656. trace_nfsd_ctl_ports_addfd(net, fd);
  657. err = nfsd_create_serv(net);
  658. if (err != 0)
  659. return err;
  660. serv = nn->nfsd_serv;
  661. err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
  662. if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
  663. nfsd_destroy_serv(net);
  664. return err;
  665. }
  666. /*
  667. * A transport listener is added by writing its transport name and
  668. * a port number.
  669. */
  670. static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
  671. {
  672. char transport[16];
  673. struct svc_xprt *xprt;
  674. int port, err;
  675. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  676. struct svc_serv *serv;
  677. if (sscanf(buf, "%15s %5u", transport, &port) != 2)
  678. return -EINVAL;
  679. if (port < 1 || port > USHRT_MAX)
  680. return -EINVAL;
  681. trace_nfsd_ctl_ports_addxprt(net, transport, port);
  682. err = nfsd_create_serv(net);
  683. if (err != 0)
  684. return err;
  685. serv = nn->nfsd_serv;
  686. err = svc_xprt_create(serv, transport, net,
  687. PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
  688. if (err < 0)
  689. goto out_err;
  690. err = svc_xprt_create(serv, transport, net,
  691. PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
  692. if (err < 0 && err != -EAFNOSUPPORT)
  693. goto out_close;
  694. return 0;
  695. out_close:
  696. xprt = svc_find_xprt(serv, transport, net, PF_INET, port);
  697. if (xprt != NULL) {
  698. svc_xprt_close(xprt);
  699. svc_xprt_put(xprt);
  700. }
  701. out_err:
  702. if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
  703. nfsd_destroy_serv(net);
  704. return err;
  705. }
  706. static ssize_t __write_ports(struct file *file, char *buf, size_t size,
  707. struct net *net)
  708. {
  709. if (size == 0)
  710. return __write_ports_names(buf, net);
  711. if (isdigit(buf[0]))
  712. return __write_ports_addfd(buf, net, file->f_cred);
  713. if (isalpha(buf[0]))
  714. return __write_ports_addxprt(buf, net, file->f_cred);
  715. return -EINVAL;
  716. }
  717. /*
  718. * write_ports - Pass a socket file descriptor or transport name to listen on
  719. *
  720. * Input:
  721. * buf: ignored
  722. * size: zero
  723. * Output:
  724. * On success: passed-in buffer filled with a '\n'-terminated C
  725. * string containing a whitespace-separated list of
  726. * named NFSD listeners;
  727. * return code is the size in bytes of the string
  728. * On error: return code is zero or a negative errno value
  729. *
  730. * OR
  731. *
  732. * Input:
  733. * buf: C string containing an unsigned
  734. * integer value representing a bound
  735. * but unconnected socket that is to be
  736. * used as an NFSD listener; listen(3)
  737. * must be called for a SOCK_STREAM
  738. * socket, otherwise it is ignored
  739. * size: non-zero length of C string in @buf
  740. * Output:
  741. * On success: NFS service is started;
  742. * passed-in buffer filled with a '\n'-terminated C
  743. * string containing a unique alphanumeric name of
  744. * the listener;
  745. * return code is the size in bytes of the string
  746. * On error: return code is a negative errno value
  747. *
  748. * OR
  749. *
  750. * Input:
  751. * buf: C string containing a transport
  752. * name and an unsigned integer value
  753. * representing the port to listen on,
  754. * separated by whitespace
  755. * size: non-zero length of C string in @buf
  756. * Output:
  757. * On success: returns zero; NFS service is started
  758. * On error: return code is a negative errno value
  759. */
  760. static ssize_t write_ports(struct file *file, char *buf, size_t size)
  761. {
  762. ssize_t rv;
  763. mutex_lock(&nfsd_mutex);
  764. rv = __write_ports(file, buf, size, netns(file));
  765. mutex_unlock(&nfsd_mutex);
  766. return rv;
  767. }
  768. int nfsd_max_blksize;
  769. /*
  770. * write_maxblksize - Set or report the current NFS blksize
  771. *
  772. * Input:
  773. * buf: ignored
  774. * size: zero
  775. *
  776. * OR
  777. *
  778. * Input:
  779. * buf: C string containing an unsigned
  780. * integer value representing the new
  781. * NFS blksize
  782. * size: non-zero length of C string in @buf
  783. * Output:
  784. * On success: passed-in buffer filled with '\n'-terminated C string
  785. * containing numeric value of the current NFS blksize
  786. * setting;
  787. * return code is the size in bytes of the string
  788. * On error: return code is zero or a negative errno value
  789. */
  790. static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
  791. {
  792. char *mesg = buf;
  793. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  794. if (size > 0) {
  795. int bsize;
  796. int rv = get_int(&mesg, &bsize);
  797. if (rv)
  798. return rv;
  799. trace_nfsd_ctl_maxblksize(netns(file), bsize);
  800. /* force bsize into allowed range and
  801. * required alignment.
  802. */
  803. bsize = max_t(int, bsize, 1024);
  804. bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
  805. bsize &= ~(1024-1);
  806. mutex_lock(&nfsd_mutex);
  807. if (nn->nfsd_serv) {
  808. mutex_unlock(&nfsd_mutex);
  809. return -EBUSY;
  810. }
  811. nfsd_max_blksize = bsize;
  812. mutex_unlock(&nfsd_mutex);
  813. }
  814. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
  815. nfsd_max_blksize);
  816. }
  817. #ifdef CONFIG_NFSD_V4
  818. static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
  819. time64_t *time, struct nfsd_net *nn)
  820. {
  821. struct dentry *dentry = file_dentry(file);
  822. char *mesg = buf;
  823. int rv, i;
  824. if (size > 0) {
  825. if (nn->nfsd_serv)
  826. return -EBUSY;
  827. rv = get_int(&mesg, &i);
  828. if (rv)
  829. return rv;
  830. trace_nfsd_ctl_time(netns(file), dentry->d_name.name,
  831. dentry->d_name.len, i);
  832. /*
  833. * Some sanity checking. We don't have a reason for
  834. * these particular numbers, but problems with the
  835. * extremes are:
  836. * - Too short: the briefest network outage may
  837. * cause clients to lose all their locks. Also,
  838. * the frequent polling may be wasteful.
  839. * - Too long: do you really want reboot recovery
  840. * to take more than an hour? Or to make other
  841. * clients wait an hour before being able to
  842. * revoke a dead client's locks?
  843. */
  844. if (i < 10 || i > 3600)
  845. return -EINVAL;
  846. *time = i;
  847. }
  848. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
  849. }
  850. static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
  851. time64_t *time, struct nfsd_net *nn)
  852. {
  853. ssize_t rv;
  854. mutex_lock(&nfsd_mutex);
  855. rv = __nfsd4_write_time(file, buf, size, time, nn);
  856. mutex_unlock(&nfsd_mutex);
  857. return rv;
  858. }
  859. /*
  860. * write_leasetime - Set or report the current NFSv4 lease time
  861. *
  862. * Input:
  863. * buf: ignored
  864. * size: zero
  865. *
  866. * OR
  867. *
  868. * Input:
  869. * buf: C string containing an unsigned
  870. * integer value representing the new
  871. * NFSv4 lease expiry time
  872. * size: non-zero length of C string in @buf
  873. * Output:
  874. * On success: passed-in buffer filled with '\n'-terminated C
  875. * string containing unsigned integer value of the
  876. * current lease expiry time;
  877. * return code is the size in bytes of the string
  878. * On error: return code is zero or a negative errno value
  879. */
  880. static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
  881. {
  882. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  883. return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
  884. }
  885. /*
  886. * write_gracetime - Set or report current NFSv4 grace period time
  887. *
  888. * As above, but sets the time of the NFSv4 grace period.
  889. *
  890. * Note this should never be set to less than the *previous*
  891. * lease-period time, but we don't try to enforce this. (In the common
  892. * case (a new boot), we don't know what the previous lease time was
  893. * anyway.)
  894. */
  895. static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
  896. {
  897. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  898. return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
  899. }
  900. #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
  901. static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
  902. struct nfsd_net *nn)
  903. {
  904. char *mesg = buf;
  905. char *recdir;
  906. int len, status;
  907. if (size > 0) {
  908. if (nn->nfsd_serv)
  909. return -EBUSY;
  910. if (size > PATH_MAX || buf[size-1] != '\n')
  911. return -EINVAL;
  912. buf[size-1] = 0;
  913. recdir = mesg;
  914. len = qword_get(&mesg, recdir, size);
  915. if (len <= 0)
  916. return -EINVAL;
  917. trace_nfsd_ctl_recoverydir(netns(file), recdir);
  918. status = nfs4_reset_recoverydir(recdir);
  919. if (status)
  920. return status;
  921. }
  922. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
  923. nfs4_recoverydir());
  924. }
  925. /*
  926. * write_recoverydir - Set or report the pathname of the recovery directory
  927. *
  928. * Input:
  929. * buf: ignored
  930. * size: zero
  931. *
  932. * OR
  933. *
  934. * Input:
  935. * buf: C string containing the pathname
  936. * of the directory on a local file
  937. * system containing permanent NFSv4
  938. * recovery data
  939. * size: non-zero length of C string in @buf
  940. * Output:
  941. * On success: passed-in buffer filled with '\n'-terminated C string
  942. * containing the current recovery pathname setting;
  943. * return code is the size in bytes of the string
  944. * On error: return code is zero or a negative errno value
  945. */
  946. static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
  947. {
  948. ssize_t rv;
  949. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  950. mutex_lock(&nfsd_mutex);
  951. rv = __write_recoverydir(file, buf, size, nn);
  952. mutex_unlock(&nfsd_mutex);
  953. return rv;
  954. }
  955. #endif
  956. /*
  957. * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
  958. *
  959. * Input:
  960. * buf: ignored
  961. * size: zero
  962. * OR
  963. *
  964. * Input:
  965. * buf: any value
  966. * size: non-zero length of C string in @buf
  967. * Output:
  968. * passed-in buffer filled with "Y" or "N" with a newline
  969. * and NULL-terminated C string. This indicates whether
  970. * the grace period has ended in the current net
  971. * namespace. Return code is the size in bytes of the
  972. * string. Writing a string that starts with 'Y', 'y', or
  973. * '1' to the file will end the grace period for nfsd's v4
  974. * lock manager.
  975. */
  976. static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
  977. {
  978. struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
  979. if (size > 0) {
  980. switch(buf[0]) {
  981. case 'Y':
  982. case 'y':
  983. case '1':
  984. if (!nfsd4_force_end_grace(nn))
  985. return -EBUSY;
  986. trace_nfsd_end_grace(netns(file));
  987. break;
  988. default:
  989. return -EINVAL;
  990. }
  991. }
  992. return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
  993. nn->grace_ended ? 'Y' : 'N');
  994. }
  995. #endif
  996. /*----------------------------------------------------------------------------*/
  997. /*
  998. * populating the filesystem.
  999. */
  1000. static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
  1001. {
  1002. struct inode *inode = new_inode(sb);
  1003. if (inode) {
  1004. /* Following advice from simple_fill_super documentation: */
  1005. inode->i_ino = iunique(sb, NFSD_MaxReserved);
  1006. inode->i_mode = mode;
  1007. simple_inode_init_ts(inode);
  1008. }
  1009. return inode;
  1010. }
  1011. static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
  1012. {
  1013. struct inode *dir = parent->d_inode;
  1014. struct dentry *dentry;
  1015. struct inode *inode;
  1016. inode = nfsd_get_inode(parent->d_sb, S_IFDIR | 0600);
  1017. if (!inode)
  1018. return ERR_PTR(-ENOMEM);
  1019. dentry = simple_start_creating(parent, name);
  1020. if (IS_ERR(dentry)) {
  1021. iput(inode);
  1022. return dentry;
  1023. }
  1024. inode->i_fop = &simple_dir_operations;
  1025. inode->i_op = &simple_dir_inode_operations;
  1026. inc_nlink(inode);
  1027. if (ncl) {
  1028. inode->i_private = ncl;
  1029. kref_get(&ncl->cl_ref);
  1030. }
  1031. d_make_persistent(dentry, inode);
  1032. inc_nlink(dir);
  1033. fsnotify_mkdir(dir, dentry);
  1034. simple_done_creating(dentry);
  1035. return dentry; // borrowed
  1036. }
  1037. #if IS_ENABLED(CONFIG_SUNRPC_GSS)
  1038. /*
  1039. * @content is assumed to be a NUL-terminated string that lives
  1040. * longer than the symlink itself.
  1041. */
  1042. static void _nfsd_symlink(struct dentry *parent, const char *name,
  1043. const char *content)
  1044. {
  1045. struct inode *dir = parent->d_inode;
  1046. struct inode *inode;
  1047. struct dentry *dentry;
  1048. inode = nfsd_get_inode(dir->i_sb, S_IFLNK | 0777);
  1049. if (!inode)
  1050. return;
  1051. dentry = simple_start_creating(parent, name);
  1052. if (IS_ERR(dentry)) {
  1053. iput(inode);
  1054. return;
  1055. }
  1056. inode->i_op = &simple_symlink_inode_operations;
  1057. inode->i_link = (char *)content;
  1058. inode->i_size = strlen(content);
  1059. d_make_persistent(dentry, inode);
  1060. fsnotify_create(dir, dentry);
  1061. simple_done_creating(dentry);
  1062. }
  1063. #else
  1064. static inline void _nfsd_symlink(struct dentry *parent, const char *name,
  1065. const char *content)
  1066. {
  1067. }
  1068. #endif
  1069. static void clear_ncl(struct dentry *dentry)
  1070. {
  1071. struct inode *inode = d_inode(dentry);
  1072. struct nfsdfs_client *ncl = inode->i_private;
  1073. spin_lock(&inode->i_lock);
  1074. inode->i_private = NULL;
  1075. spin_unlock(&inode->i_lock);
  1076. kref_put(&ncl->cl_ref, ncl->cl_release);
  1077. }
  1078. struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
  1079. {
  1080. struct nfsdfs_client *nc;
  1081. spin_lock(&inode->i_lock);
  1082. nc = inode->i_private;
  1083. if (nc)
  1084. kref_get(&nc->cl_ref);
  1085. spin_unlock(&inode->i_lock);
  1086. return nc;
  1087. }
  1088. /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
  1089. * code instead. */
  1090. static int nfsdfs_create_files(struct dentry *root,
  1091. const struct tree_descr *files,
  1092. struct nfsdfs_client *ncl,
  1093. struct dentry **fdentries)
  1094. {
  1095. struct inode *dir = d_inode(root);
  1096. struct dentry *dentry;
  1097. for (int i = 0; files->name && files->name[0]; i++, files++) {
  1098. struct inode *inode = nfsd_get_inode(root->d_sb,
  1099. S_IFREG | files->mode);
  1100. if (!inode)
  1101. return -ENOMEM;
  1102. dentry = simple_start_creating(root, files->name);
  1103. if (IS_ERR(dentry)) {
  1104. iput(inode);
  1105. return PTR_ERR(dentry);
  1106. }
  1107. kref_get(&ncl->cl_ref);
  1108. inode->i_fop = files->ops;
  1109. inode->i_private = ncl;
  1110. d_make_persistent(dentry, inode);
  1111. fsnotify_create(dir, dentry);
  1112. if (fdentries)
  1113. fdentries[i] = dentry; // borrowed
  1114. simple_done_creating(dentry);
  1115. }
  1116. return 0;
  1117. }
  1118. /* on success, returns positive number unique to that client. */
  1119. struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
  1120. struct nfsdfs_client *ncl, u32 id,
  1121. const struct tree_descr *files,
  1122. struct dentry **fdentries)
  1123. {
  1124. struct dentry *dentry;
  1125. char name[11];
  1126. int ret;
  1127. sprintf(name, "%u", id);
  1128. dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
  1129. if (IS_ERR(dentry)) /* XXX: tossing errors? */
  1130. return NULL;
  1131. ret = nfsdfs_create_files(dentry, files, ncl, fdentries);
  1132. if (ret) {
  1133. nfsd_client_rmdir(dentry);
  1134. return NULL;
  1135. }
  1136. return dentry;
  1137. }
  1138. /* Taken from __rpc_rmdir: */
  1139. void nfsd_client_rmdir(struct dentry *dentry)
  1140. {
  1141. simple_recursive_removal(dentry, clear_ncl);
  1142. }
  1143. static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
  1144. {
  1145. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  1146. nfsd_net_id);
  1147. struct dentry *dentry;
  1148. int ret;
  1149. static const struct tree_descr nfsd_files[] = {
  1150. [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
  1151. /* Per-export io stats use same ops as exports file */
  1152. [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
  1153. [NFSD_Export_features] = {"export_features",
  1154. &export_features_fops, S_IRUGO},
  1155. [NFSD_FO_UnlockIP] = {"unlock_ip",
  1156. &transaction_ops, S_IWUSR|S_IRUSR},
  1157. [NFSD_FO_UnlockFS] = {"unlock_filesystem",
  1158. &transaction_ops, S_IWUSR|S_IRUSR},
  1159. [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
  1160. [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
  1161. [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
  1162. [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
  1163. [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
  1164. &nfsd_reply_cache_stats_fops, S_IRUGO},
  1165. [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
  1166. [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
  1167. [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
  1168. [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
  1169. #ifdef CONFIG_NFSD_V4
  1170. [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
  1171. [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
  1172. #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
  1173. [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
  1174. #endif
  1175. [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
  1176. #endif
  1177. /* last one */ {""}
  1178. };
  1179. ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
  1180. if (ret)
  1181. return ret;
  1182. _nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
  1183. "/proc/net/rpc/gss_krb5_enctypes");
  1184. dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
  1185. if (IS_ERR(dentry))
  1186. return PTR_ERR(dentry);
  1187. nn->nfsd_client_dir = dentry;
  1188. return 0;
  1189. }
  1190. static int nfsd_fs_get_tree(struct fs_context *fc)
  1191. {
  1192. return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
  1193. }
  1194. static void nfsd_fs_free_fc(struct fs_context *fc)
  1195. {
  1196. if (fc->s_fs_info)
  1197. put_net(fc->s_fs_info);
  1198. }
  1199. static const struct fs_context_operations nfsd_fs_context_ops = {
  1200. .free = nfsd_fs_free_fc,
  1201. .get_tree = nfsd_fs_get_tree,
  1202. };
  1203. static int nfsd_init_fs_context(struct fs_context *fc)
  1204. {
  1205. put_user_ns(fc->user_ns);
  1206. fc->user_ns = get_user_ns(fc->net_ns->user_ns);
  1207. fc->ops = &nfsd_fs_context_ops;
  1208. return 0;
  1209. }
  1210. static void nfsd_umount(struct super_block *sb)
  1211. {
  1212. struct net *net = sb->s_fs_info;
  1213. nfsd_shutdown_threads(net);
  1214. kill_anon_super(sb);
  1215. put_net(net);
  1216. }
  1217. static struct file_system_type nfsd_fs_type = {
  1218. .owner = THIS_MODULE,
  1219. .name = "nfsd",
  1220. .init_fs_context = nfsd_init_fs_context,
  1221. .kill_sb = nfsd_umount,
  1222. };
  1223. MODULE_ALIAS_FS("nfsd");
  1224. #ifdef CONFIG_PROC_FS
  1225. static int exports_proc_open(struct inode *inode, struct file *file)
  1226. {
  1227. return exports_net_open(current->nsproxy->net_ns, file);
  1228. }
  1229. static const struct proc_ops exports_proc_ops = {
  1230. .proc_open = exports_proc_open,
  1231. .proc_read = seq_read,
  1232. .proc_lseek = seq_lseek,
  1233. .proc_release = exports_release,
  1234. };
  1235. static int create_proc_exports_entry(void)
  1236. {
  1237. struct proc_dir_entry *entry;
  1238. entry = proc_mkdir("fs/nfs", NULL);
  1239. if (!entry)
  1240. return -ENOMEM;
  1241. entry = proc_create("exports", 0, entry, &exports_proc_ops);
  1242. if (!entry) {
  1243. remove_proc_entry("fs/nfs", NULL);
  1244. return -ENOMEM;
  1245. }
  1246. return 0;
  1247. }
  1248. #else /* CONFIG_PROC_FS */
  1249. static int create_proc_exports_entry(void)
  1250. {
  1251. return 0;
  1252. }
  1253. #endif
  1254. unsigned int nfsd_net_id;
  1255. static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb,
  1256. struct netlink_callback *cb,
  1257. struct nfsd_genl_rqstp *genl_rqstp)
  1258. {
  1259. void *hdr;
  1260. u32 i;
  1261. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1262. &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET);
  1263. if (!hdr)
  1264. return -ENOBUFS;
  1265. if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, genl_rqstp->rq_xid) ||
  1266. nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, genl_rqstp->rq_flags) ||
  1267. nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, genl_rqstp->rq_prog) ||
  1268. nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, genl_rqstp->rq_proc) ||
  1269. nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, genl_rqstp->rq_vers) ||
  1270. nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME,
  1271. ktime_to_us(genl_rqstp->rq_stime),
  1272. NFSD_A_RPC_STATUS_PAD))
  1273. return -ENOBUFS;
  1274. switch (genl_rqstp->rq_saddr.sa_family) {
  1275. case AF_INET: {
  1276. const struct sockaddr_in *s_in, *d_in;
  1277. s_in = (const struct sockaddr_in *)&genl_rqstp->rq_saddr;
  1278. d_in = (const struct sockaddr_in *)&genl_rqstp->rq_daddr;
  1279. if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4,
  1280. s_in->sin_addr.s_addr) ||
  1281. nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4,
  1282. d_in->sin_addr.s_addr) ||
  1283. nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
  1284. s_in->sin_port) ||
  1285. nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
  1286. d_in->sin_port))
  1287. return -ENOBUFS;
  1288. break;
  1289. }
  1290. case AF_INET6: {
  1291. const struct sockaddr_in6 *s_in, *d_in;
  1292. s_in = (const struct sockaddr_in6 *)&genl_rqstp->rq_saddr;
  1293. d_in = (const struct sockaddr_in6 *)&genl_rqstp->rq_daddr;
  1294. if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6,
  1295. &s_in->sin6_addr) ||
  1296. nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6,
  1297. &d_in->sin6_addr) ||
  1298. nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
  1299. s_in->sin6_port) ||
  1300. nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
  1301. d_in->sin6_port))
  1302. return -ENOBUFS;
  1303. break;
  1304. }
  1305. }
  1306. for (i = 0; i < genl_rqstp->rq_opcnt; i++)
  1307. if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS,
  1308. genl_rqstp->rq_opnum[i]))
  1309. return -ENOBUFS;
  1310. genlmsg_end(skb, hdr);
  1311. return 0;
  1312. }
  1313. /**
  1314. * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit
  1315. * @skb: reply buffer
  1316. * @cb: netlink metadata and command arguments
  1317. *
  1318. * Returns the size of the reply or a negative errno.
  1319. */
  1320. int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb,
  1321. struct netlink_callback *cb)
  1322. {
  1323. int i, ret, rqstp_index = 0;
  1324. struct nfsd_net *nn;
  1325. mutex_lock(&nfsd_mutex);
  1326. nn = net_generic(sock_net(skb->sk), nfsd_net_id);
  1327. if (!nn->nfsd_serv) {
  1328. ret = -ENODEV;
  1329. goto out_unlock;
  1330. }
  1331. rcu_read_lock();
  1332. for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) {
  1333. struct svc_rqst *rqstp;
  1334. if (i < cb->args[0]) /* already consumed */
  1335. continue;
  1336. rqstp_index = 0;
  1337. list_for_each_entry_rcu(rqstp,
  1338. &nn->nfsd_serv->sv_pools[i].sp_all_threads,
  1339. rq_all) {
  1340. struct nfsd_genl_rqstp genl_rqstp;
  1341. unsigned int status_counter;
  1342. if (rqstp_index++ < cb->args[1]) /* already consumed */
  1343. continue;
  1344. /*
  1345. * Acquire rq_status_counter before parsing the rqst
  1346. * fields. rq_status_counter is set to an odd value in
  1347. * order to notify the consumers the rqstp fields are
  1348. * meaningful.
  1349. */
  1350. status_counter =
  1351. smp_load_acquire(&rqstp->rq_status_counter);
  1352. if (!(status_counter & 1))
  1353. continue;
  1354. genl_rqstp.rq_xid = rqstp->rq_xid;
  1355. genl_rqstp.rq_flags = rqstp->rq_flags;
  1356. genl_rqstp.rq_vers = rqstp->rq_vers;
  1357. genl_rqstp.rq_prog = rqstp->rq_prog;
  1358. genl_rqstp.rq_proc = rqstp->rq_proc;
  1359. genl_rqstp.rq_stime = rqstp->rq_stime;
  1360. genl_rqstp.rq_opcnt = 0;
  1361. memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp),
  1362. sizeof(struct sockaddr));
  1363. memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp),
  1364. sizeof(struct sockaddr));
  1365. #ifdef CONFIG_NFSD_V4
  1366. if (rqstp->rq_vers == NFS4_VERSION &&
  1367. rqstp->rq_proc == NFSPROC4_COMPOUND) {
  1368. /* NFSv4 compound */
  1369. struct nfsd4_compoundargs *args;
  1370. int j;
  1371. args = rqstp->rq_argp;
  1372. genl_rqstp.rq_opcnt = min_t(u32, args->opcnt,
  1373. ARRAY_SIZE(genl_rqstp.rq_opnum));
  1374. for (j = 0; j < genl_rqstp.rq_opcnt; j++)
  1375. genl_rqstp.rq_opnum[j] =
  1376. args->ops[j].opnum;
  1377. }
  1378. #endif /* CONFIG_NFSD_V4 */
  1379. /*
  1380. * Acquire rq_status_counter before reporting the rqst
  1381. * fields to the user.
  1382. */
  1383. if (smp_load_acquire(&rqstp->rq_status_counter) !=
  1384. status_counter)
  1385. continue;
  1386. ret = nfsd_genl_rpc_status_compose_msg(skb, cb,
  1387. &genl_rqstp);
  1388. if (ret)
  1389. goto out;
  1390. }
  1391. }
  1392. cb->args[0] = i;
  1393. cb->args[1] = rqstp_index;
  1394. ret = skb->len;
  1395. out:
  1396. rcu_read_unlock();
  1397. out_unlock:
  1398. mutex_unlock(&nfsd_mutex);
  1399. return ret;
  1400. }
  1401. /**
  1402. * nfsd_nl_threads_set_doit - set the number of running threads
  1403. * @skb: reply buffer
  1404. * @info: netlink metadata and command arguments
  1405. *
  1406. * Return 0 on success or a negative errno.
  1407. */
  1408. int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info)
  1409. {
  1410. int *nthreads, nrpools = 0, i, ret = -EOPNOTSUPP, rem;
  1411. struct net *net = genl_info_net(info);
  1412. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1413. const struct nlattr *attr;
  1414. const char *scope = NULL;
  1415. if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS))
  1416. return -EINVAL;
  1417. /* count number of SERVER_THREADS values */
  1418. nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_THREADS, info->nlhdr,
  1419. GENL_HDRLEN, rem)
  1420. nrpools++;
  1421. mutex_lock(&nfsd_mutex);
  1422. nthreads = kzalloc_objs(int, nrpools);
  1423. if (!nthreads) {
  1424. ret = -ENOMEM;
  1425. goto out_unlock;
  1426. }
  1427. i = 0;
  1428. nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_THREADS, info->nlhdr,
  1429. GENL_HDRLEN, rem) {
  1430. nthreads[i++] = nla_get_u32(attr);
  1431. if (i >= nrpools)
  1432. break;
  1433. }
  1434. if (info->attrs[NFSD_A_SERVER_GRACETIME] ||
  1435. info->attrs[NFSD_A_SERVER_LEASETIME] ||
  1436. info->attrs[NFSD_A_SERVER_SCOPE]) {
  1437. ret = -EBUSY;
  1438. if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads)
  1439. goto out_unlock;
  1440. ret = -EINVAL;
  1441. attr = info->attrs[NFSD_A_SERVER_GRACETIME];
  1442. if (attr) {
  1443. u32 gracetime = nla_get_u32(attr);
  1444. if (gracetime < 10 || gracetime > 3600)
  1445. goto out_unlock;
  1446. nn->nfsd4_grace = gracetime;
  1447. }
  1448. attr = info->attrs[NFSD_A_SERVER_LEASETIME];
  1449. if (attr) {
  1450. u32 leasetime = nla_get_u32(attr);
  1451. if (leasetime < 10 || leasetime > 3600)
  1452. goto out_unlock;
  1453. nn->nfsd4_lease = leasetime;
  1454. }
  1455. attr = info->attrs[NFSD_A_SERVER_SCOPE];
  1456. if (attr)
  1457. scope = nla_data(attr);
  1458. }
  1459. attr = info->attrs[NFSD_A_SERVER_MIN_THREADS];
  1460. if (attr)
  1461. nn->min_threads = nla_get_u32(attr);
  1462. ret = nfsd_svc(nrpools, nthreads, net, current_cred(), scope);
  1463. if (ret > 0)
  1464. ret = 0;
  1465. out_unlock:
  1466. mutex_unlock(&nfsd_mutex);
  1467. kfree(nthreads);
  1468. return ret;
  1469. }
  1470. /**
  1471. * nfsd_nl_threads_get_doit - get the maximum number of running threads
  1472. * @skb: reply buffer
  1473. * @info: netlink metadata and command arguments
  1474. *
  1475. * Return 0 on success or a negative errno.
  1476. */
  1477. int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info)
  1478. {
  1479. struct net *net = genl_info_net(info);
  1480. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1481. void *hdr;
  1482. int err;
  1483. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1484. if (!skb)
  1485. return -ENOMEM;
  1486. hdr = genlmsg_iput(skb, info);
  1487. if (!hdr) {
  1488. err = -EMSGSIZE;
  1489. goto err_free_msg;
  1490. }
  1491. mutex_lock(&nfsd_mutex);
  1492. err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME,
  1493. nn->nfsd4_grace) ||
  1494. nla_put_u32(skb, NFSD_A_SERVER_LEASETIME,
  1495. nn->nfsd4_lease) ||
  1496. nla_put_u32(skb, NFSD_A_SERVER_MIN_THREADS,
  1497. nn->min_threads) ||
  1498. nla_put_string(skb, NFSD_A_SERVER_SCOPE,
  1499. nn->nfsd_name);
  1500. if (err)
  1501. goto err_unlock;
  1502. if (nn->nfsd_serv) {
  1503. int i;
  1504. for (i = 0; i < nfsd_nrpools(net); ++i) {
  1505. struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i];
  1506. err = nla_put_u32(skb, NFSD_A_SERVER_THREADS,
  1507. sp->sp_nrthrmax);
  1508. if (err)
  1509. goto err_unlock;
  1510. }
  1511. } else {
  1512. err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0);
  1513. if (err)
  1514. goto err_unlock;
  1515. }
  1516. mutex_unlock(&nfsd_mutex);
  1517. genlmsg_end(skb, hdr);
  1518. return genlmsg_reply(skb, info);
  1519. err_unlock:
  1520. mutex_unlock(&nfsd_mutex);
  1521. err_free_msg:
  1522. nlmsg_free(skb);
  1523. return err;
  1524. }
  1525. /**
  1526. * nfsd_nl_version_set_doit - set the nfs enabled versions
  1527. * @skb: reply buffer
  1528. * @info: netlink metadata and command arguments
  1529. *
  1530. * Return 0 on success or a negative errno.
  1531. */
  1532. int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info)
  1533. {
  1534. const struct nlattr *attr;
  1535. struct nfsd_net *nn;
  1536. int i, rem;
  1537. if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION))
  1538. return -EINVAL;
  1539. mutex_lock(&nfsd_mutex);
  1540. nn = net_generic(genl_info_net(info), nfsd_net_id);
  1541. if (nn->nfsd_serv) {
  1542. mutex_unlock(&nfsd_mutex);
  1543. return -EBUSY;
  1544. }
  1545. /* clear current supported versions. */
  1546. nfsd_vers(nn, 2, NFSD_CLEAR);
  1547. nfsd_vers(nn, 3, NFSD_CLEAR);
  1548. for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
  1549. nfsd_minorversion(nn, i, NFSD_CLEAR);
  1550. nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_PROTO_VERSION, info->nlhdr,
  1551. GENL_HDRLEN, rem) {
  1552. struct nlattr *tb[NFSD_A_VERSION_MAX + 1];
  1553. u32 major, minor = 0;
  1554. bool enabled;
  1555. if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr,
  1556. nfsd_version_nl_policy, info->extack) < 0)
  1557. continue;
  1558. if (!tb[NFSD_A_VERSION_MAJOR])
  1559. continue;
  1560. major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]);
  1561. if (tb[NFSD_A_VERSION_MINOR])
  1562. minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]);
  1563. enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]);
  1564. switch (major) {
  1565. case 4:
  1566. nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR);
  1567. break;
  1568. case 3:
  1569. case 2:
  1570. if (!minor)
  1571. nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR);
  1572. break;
  1573. default:
  1574. break;
  1575. }
  1576. }
  1577. mutex_unlock(&nfsd_mutex);
  1578. return 0;
  1579. }
  1580. /**
  1581. * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions
  1582. * @skb: reply buffer
  1583. * @info: netlink metadata and command arguments
  1584. *
  1585. * Return 0 on success or a negative errno.
  1586. */
  1587. int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info)
  1588. {
  1589. struct nfsd_net *nn;
  1590. int i, err;
  1591. void *hdr;
  1592. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1593. if (!skb)
  1594. return -ENOMEM;
  1595. hdr = genlmsg_iput(skb, info);
  1596. if (!hdr) {
  1597. err = -EMSGSIZE;
  1598. goto err_free_msg;
  1599. }
  1600. mutex_lock(&nfsd_mutex);
  1601. nn = net_generic(genl_info_net(info), nfsd_net_id);
  1602. for (i = 2; i <= 4; i++) {
  1603. int j;
  1604. for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) {
  1605. struct nlattr *attr;
  1606. /* Don't record any versions the kernel doesn't have
  1607. * compiled in
  1608. */
  1609. if (!nfsd_support_version(i))
  1610. continue;
  1611. /* NFSv{2,3} does not support minor numbers */
  1612. if (i < 4 && j)
  1613. continue;
  1614. attr = nla_nest_start(skb,
  1615. NFSD_A_SERVER_PROTO_VERSION);
  1616. if (!attr) {
  1617. err = -EINVAL;
  1618. goto err_nfsd_unlock;
  1619. }
  1620. if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) ||
  1621. nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) {
  1622. err = -EINVAL;
  1623. goto err_nfsd_unlock;
  1624. }
  1625. /* Set the enabled flag if the version is enabled */
  1626. if (nfsd_vers(nn, i, NFSD_TEST) &&
  1627. (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) &&
  1628. nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) {
  1629. err = -EINVAL;
  1630. goto err_nfsd_unlock;
  1631. }
  1632. nla_nest_end(skb, attr);
  1633. }
  1634. }
  1635. mutex_unlock(&nfsd_mutex);
  1636. genlmsg_end(skb, hdr);
  1637. return genlmsg_reply(skb, info);
  1638. err_nfsd_unlock:
  1639. mutex_unlock(&nfsd_mutex);
  1640. err_free_msg:
  1641. nlmsg_free(skb);
  1642. return err;
  1643. }
  1644. /**
  1645. * nfsd_nl_listener_set_doit - set the nfs running sockets
  1646. * @skb: reply buffer
  1647. * @info: netlink metadata and command arguments
  1648. *
  1649. * Return 0 on success or a negative errno.
  1650. */
  1651. int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info)
  1652. {
  1653. struct net *net = genl_info_net(info);
  1654. struct svc_xprt *xprt, *tmp;
  1655. const struct nlattr *attr;
  1656. struct svc_serv *serv;
  1657. LIST_HEAD(permsocks);
  1658. struct nfsd_net *nn;
  1659. bool delete = false;
  1660. int err, rem;
  1661. mutex_lock(&nfsd_mutex);
  1662. err = nfsd_create_serv(net);
  1663. if (err) {
  1664. mutex_unlock(&nfsd_mutex);
  1665. return err;
  1666. }
  1667. nn = net_generic(net, nfsd_net_id);
  1668. serv = nn->nfsd_serv;
  1669. spin_lock_bh(&serv->sv_lock);
  1670. /* Move all of the old listener sockets to a temp list */
  1671. list_splice_init(&serv->sv_permsocks, &permsocks);
  1672. /*
  1673. * Walk the list of server_socks from userland and move any that match
  1674. * back to sv_permsocks
  1675. */
  1676. nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr,
  1677. GENL_HDRLEN, rem) {
  1678. struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
  1679. const char *xcl_name;
  1680. struct sockaddr *sa;
  1681. if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
  1682. nfsd_sock_nl_policy, info->extack) < 0)
  1683. continue;
  1684. if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
  1685. continue;
  1686. if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
  1687. continue;
  1688. xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
  1689. sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
  1690. /* Put back any matching sockets */
  1691. list_for_each_entry_safe(xprt, tmp, &permsocks, xpt_list) {
  1692. /* This shouldn't be possible */
  1693. if (WARN_ON_ONCE(xprt->xpt_net != net)) {
  1694. list_move(&xprt->xpt_list, &serv->sv_permsocks);
  1695. continue;
  1696. }
  1697. /* If everything matches, put it back */
  1698. if (!strcmp(xprt->xpt_class->xcl_name, xcl_name) &&
  1699. rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local)) {
  1700. list_move(&xprt->xpt_list, &serv->sv_permsocks);
  1701. break;
  1702. }
  1703. }
  1704. }
  1705. /*
  1706. * If there are listener transports remaining on the permsocks list,
  1707. * it means we were asked to remove a listener.
  1708. */
  1709. if (!list_empty(&permsocks)) {
  1710. list_splice_init(&permsocks, &serv->sv_permsocks);
  1711. delete = true;
  1712. }
  1713. spin_unlock_bh(&serv->sv_lock);
  1714. /* Do not remove listeners while there are active threads. */
  1715. if (serv->sv_nrthreads) {
  1716. err = -EBUSY;
  1717. goto out_unlock_mtx;
  1718. }
  1719. /*
  1720. * Since we can't delete an arbitrary llist entry, destroy the
  1721. * remaining listeners and recreate the list.
  1722. */
  1723. if (delete)
  1724. svc_xprt_destroy_all(serv, net, false);
  1725. /* walk list of addrs again, open any that still don't exist */
  1726. nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr,
  1727. GENL_HDRLEN, rem) {
  1728. struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
  1729. const char *xcl_name;
  1730. struct sockaddr *sa;
  1731. int ret;
  1732. if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
  1733. nfsd_sock_nl_policy, info->extack) < 0)
  1734. continue;
  1735. if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
  1736. continue;
  1737. if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
  1738. continue;
  1739. xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
  1740. sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
  1741. xprt = svc_find_listener(serv, xcl_name, net, sa);
  1742. if (xprt) {
  1743. if (delete)
  1744. WARN_ONCE(1, "Transport type=%s already exists\n",
  1745. xcl_name);
  1746. svc_xprt_put(xprt);
  1747. continue;
  1748. }
  1749. ret = svc_xprt_create_from_sa(serv, xcl_name, net, sa, 0,
  1750. current_cred());
  1751. /* always save the latest error */
  1752. if (ret < 0)
  1753. err = ret;
  1754. }
  1755. if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
  1756. nfsd_destroy_serv(net);
  1757. out_unlock_mtx:
  1758. mutex_unlock(&nfsd_mutex);
  1759. return err;
  1760. }
  1761. /**
  1762. * nfsd_nl_listener_get_doit - get the nfs running listeners
  1763. * @skb: reply buffer
  1764. * @info: netlink metadata and command arguments
  1765. *
  1766. * Return 0 on success or a negative errno.
  1767. */
  1768. int nfsd_nl_listener_get_doit(struct sk_buff *skb, struct genl_info *info)
  1769. {
  1770. struct svc_xprt *xprt;
  1771. struct svc_serv *serv;
  1772. struct nfsd_net *nn;
  1773. void *hdr;
  1774. int err;
  1775. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1776. if (!skb)
  1777. return -ENOMEM;
  1778. hdr = genlmsg_iput(skb, info);
  1779. if (!hdr) {
  1780. err = -EMSGSIZE;
  1781. goto err_free_msg;
  1782. }
  1783. mutex_lock(&nfsd_mutex);
  1784. nn = net_generic(genl_info_net(info), nfsd_net_id);
  1785. /* no nfs server? Just send empty socket list */
  1786. if (!nn->nfsd_serv)
  1787. goto out_unlock_mtx;
  1788. serv = nn->nfsd_serv;
  1789. spin_lock_bh(&serv->sv_lock);
  1790. list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
  1791. struct nlattr *attr;
  1792. attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR);
  1793. if (!attr) {
  1794. err = -EINVAL;
  1795. goto err_serv_unlock;
  1796. }
  1797. if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME,
  1798. xprt->xpt_class->xcl_name) ||
  1799. nla_put(skb, NFSD_A_SOCK_ADDR,
  1800. sizeof(struct sockaddr_storage),
  1801. &xprt->xpt_local)) {
  1802. err = -EINVAL;
  1803. goto err_serv_unlock;
  1804. }
  1805. nla_nest_end(skb, attr);
  1806. }
  1807. spin_unlock_bh(&serv->sv_lock);
  1808. out_unlock_mtx:
  1809. mutex_unlock(&nfsd_mutex);
  1810. genlmsg_end(skb, hdr);
  1811. return genlmsg_reply(skb, info);
  1812. err_serv_unlock:
  1813. spin_unlock_bh(&serv->sv_lock);
  1814. mutex_unlock(&nfsd_mutex);
  1815. err_free_msg:
  1816. nlmsg_free(skb);
  1817. return err;
  1818. }
  1819. /**
  1820. * nfsd_nl_pool_mode_set_doit - set the number of running threads
  1821. * @skb: reply buffer
  1822. * @info: netlink metadata and command arguments
  1823. *
  1824. * Return 0 on success or a negative errno.
  1825. */
  1826. int nfsd_nl_pool_mode_set_doit(struct sk_buff *skb, struct genl_info *info)
  1827. {
  1828. const struct nlattr *attr;
  1829. if (GENL_REQ_ATTR_CHECK(info, NFSD_A_POOL_MODE_MODE))
  1830. return -EINVAL;
  1831. attr = info->attrs[NFSD_A_POOL_MODE_MODE];
  1832. return sunrpc_set_pool_mode(nla_data(attr));
  1833. }
  1834. /**
  1835. * nfsd_nl_pool_mode_get_doit - get info about pool_mode
  1836. * @skb: reply buffer
  1837. * @info: netlink metadata and command arguments
  1838. *
  1839. * Return 0 on success or a negative errno.
  1840. */
  1841. int nfsd_nl_pool_mode_get_doit(struct sk_buff *skb, struct genl_info *info)
  1842. {
  1843. struct net *net = genl_info_net(info);
  1844. char buf[16];
  1845. void *hdr;
  1846. int err;
  1847. if (sunrpc_get_pool_mode(buf, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
  1848. return -ERANGE;
  1849. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1850. if (!skb)
  1851. return -ENOMEM;
  1852. err = -EMSGSIZE;
  1853. hdr = genlmsg_iput(skb, info);
  1854. if (!hdr)
  1855. goto err_free_msg;
  1856. err = nla_put_string(skb, NFSD_A_POOL_MODE_MODE, buf) |
  1857. nla_put_u32(skb, NFSD_A_POOL_MODE_NPOOLS, nfsd_nrpools(net));
  1858. if (err)
  1859. goto err_free_msg;
  1860. genlmsg_end(skb, hdr);
  1861. return genlmsg_reply(skb, info);
  1862. err_free_msg:
  1863. nlmsg_free(skb);
  1864. return err;
  1865. }
  1866. /**
  1867. * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
  1868. * @net: a freshly-created network namespace
  1869. *
  1870. * This information stays around as long as the network namespace is
  1871. * alive whether or not there is an NFSD instance running in the
  1872. * namespace.
  1873. *
  1874. * Returns zero on success, or a negative errno otherwise.
  1875. */
  1876. static __net_init int nfsd_net_init(struct net *net)
  1877. {
  1878. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1879. int retval;
  1880. int i;
  1881. retval = nfsd_export_init(net);
  1882. if (retval)
  1883. goto out_export_error;
  1884. retval = nfsd_idmap_init(net);
  1885. if (retval)
  1886. goto out_idmap_error;
  1887. retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL,
  1888. NFSD_STATS_COUNTERS_NUM);
  1889. if (retval)
  1890. goto out_repcache_error;
  1891. memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats));
  1892. nn->nfsd_svcstats.program = &nfsd_programs[0];
  1893. if (!nfsd_proc_stat_init(net)) {
  1894. retval = -ENOMEM;
  1895. goto out_proc_error;
  1896. }
  1897. for (i = 0; i < sizeof(nn->nfsd_versions); i++)
  1898. nn->nfsd_versions[i] = nfsd_support_version(i);
  1899. for (i = 0; i < sizeof(nn->nfsd4_minorversions); i++)
  1900. nn->nfsd4_minorversions[i] = nfsd_support_version(4);
  1901. nn->nfsd_info.mutex = &nfsd_mutex;
  1902. nn->nfsd_serv = NULL;
  1903. nfsd4_init_leases_net(nn);
  1904. get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
  1905. seqlock_init(&nn->writeverf_lock);
  1906. #if IS_ENABLED(CONFIG_NFS_LOCALIO)
  1907. spin_lock_init(&nn->local_clients_lock);
  1908. INIT_LIST_HEAD(&nn->local_clients);
  1909. #endif
  1910. return 0;
  1911. out_proc_error:
  1912. percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
  1913. out_repcache_error:
  1914. nfsd_idmap_shutdown(net);
  1915. out_idmap_error:
  1916. nfsd_export_shutdown(net);
  1917. out_export_error:
  1918. return retval;
  1919. }
  1920. #if IS_ENABLED(CONFIG_NFS_LOCALIO)
  1921. /**
  1922. * nfsd_net_pre_exit - Disconnect localio clients from net namespace
  1923. * @net: a network namespace that is about to be destroyed
  1924. *
  1925. * This invalidates ->net pointers held by localio clients
  1926. * while they can still safely access nn->counter.
  1927. */
  1928. static __net_exit void nfsd_net_pre_exit(struct net *net)
  1929. {
  1930. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1931. nfs_localio_invalidate_clients(&nn->local_clients,
  1932. &nn->local_clients_lock);
  1933. }
  1934. #endif
  1935. /**
  1936. * nfsd_net_exit - Release the nfsd_net portion of a net namespace
  1937. * @net: a network namespace that is about to be destroyed
  1938. *
  1939. */
  1940. static __net_exit void nfsd_net_exit(struct net *net)
  1941. {
  1942. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1943. nfsd_proc_stat_shutdown(net);
  1944. percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
  1945. nfsd_idmap_shutdown(net);
  1946. nfsd_export_shutdown(net);
  1947. }
  1948. static struct pernet_operations nfsd_net_ops = {
  1949. .init = nfsd_net_init,
  1950. #if IS_ENABLED(CONFIG_NFS_LOCALIO)
  1951. .pre_exit = nfsd_net_pre_exit,
  1952. #endif
  1953. .exit = nfsd_net_exit,
  1954. .id = &nfsd_net_id,
  1955. .size = sizeof(struct nfsd_net),
  1956. };
  1957. static int __init init_nfsd(void)
  1958. {
  1959. int retval;
  1960. nfsd_debugfs_init();
  1961. retval = nfsd4_init_slabs();
  1962. if (retval)
  1963. return retval;
  1964. retval = nfsd4_init_pnfs();
  1965. if (retval)
  1966. goto out_free_slabs;
  1967. retval = nfsd_drc_slab_create();
  1968. if (retval)
  1969. goto out_free_pnfs;
  1970. nfsd_lockd_init(); /* lockd->nfsd callbacks */
  1971. retval = nfsd_export_wq_init();
  1972. if (retval)
  1973. goto out_free_lockd;
  1974. retval = register_pernet_subsys(&nfsd_net_ops);
  1975. if (retval < 0)
  1976. goto out_free_export_wq;
  1977. retval = register_cld_notifier();
  1978. if (retval)
  1979. goto out_free_subsys;
  1980. retval = nfsd4_create_laundry_wq();
  1981. if (retval)
  1982. goto out_free_cld;
  1983. retval = register_filesystem(&nfsd_fs_type);
  1984. if (retval)
  1985. goto out_free_nfsd4;
  1986. retval = genl_register_family(&nfsd_nl_family);
  1987. if (retval)
  1988. goto out_free_filesystem;
  1989. retval = create_proc_exports_entry();
  1990. if (retval)
  1991. goto out_free_all;
  1992. nfsd_localio_ops_init();
  1993. return 0;
  1994. out_free_all:
  1995. genl_unregister_family(&nfsd_nl_family);
  1996. out_free_filesystem:
  1997. unregister_filesystem(&nfsd_fs_type);
  1998. out_free_nfsd4:
  1999. nfsd4_destroy_laundry_wq();
  2000. out_free_cld:
  2001. unregister_cld_notifier();
  2002. out_free_subsys:
  2003. unregister_pernet_subsys(&nfsd_net_ops);
  2004. out_free_export_wq:
  2005. nfsd_export_wq_shutdown();
  2006. out_free_lockd:
  2007. nfsd_lockd_shutdown();
  2008. nfsd_drc_slab_free();
  2009. out_free_pnfs:
  2010. nfsd4_exit_pnfs();
  2011. out_free_slabs:
  2012. nfsd4_free_slabs();
  2013. nfsd_debugfs_exit();
  2014. return retval;
  2015. }
  2016. static void __exit exit_nfsd(void)
  2017. {
  2018. remove_proc_entry("fs/nfs/exports", NULL);
  2019. remove_proc_entry("fs/nfs", NULL);
  2020. genl_unregister_family(&nfsd_nl_family);
  2021. unregister_filesystem(&nfsd_fs_type);
  2022. nfsd4_destroy_laundry_wq();
  2023. unregister_cld_notifier();
  2024. unregister_pernet_subsys(&nfsd_net_ops);
  2025. nfsd_export_wq_shutdown();
  2026. nfsd_drc_slab_free();
  2027. nfsd_lockd_shutdown();
  2028. nfsd4_free_slabs();
  2029. nfsd4_exit_pnfs();
  2030. nfsd_debugfs_exit();
  2031. }
  2032. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  2033. MODULE_DESCRIPTION("In-kernel NFS server");
  2034. MODULE_LICENSE("GPL");
  2035. module_init(init_nfsd)
  2036. module_exit(exit_nfsd)