nfsctl.c 58 KB

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