vfs.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * File operations used by nfsd. Some of these have been ripped from
  4. * other parts of the kernel because they weren't exported, others
  5. * are partial duplicates with added or changed functionality.
  6. *
  7. * Note that several functions dget() the dentry upon which they want
  8. * to act, most notably those that create directory entries. Response
  9. * dentry's are dput()'d if necessary in the release callback.
  10. * So if you notice code paths that apparently fail to dput() the
  11. * dentry, don't worry--they have been taken care of.
  12. *
  13. * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
  14. * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
  15. */
  16. #include <linux/fs.h>
  17. #include <linux/file.h>
  18. #include <linux/splice.h>
  19. #include <linux/falloc.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/namei.h>
  22. #include <linux/delay.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include <linux/xattr.h>
  26. #include <linux/jhash.h>
  27. #include <linux/ima.h>
  28. #include <linux/slab.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/exportfs.h>
  31. #include <linux/writeback.h>
  32. #include <linux/security.h>
  33. #ifdef CONFIG_NFSD_V3
  34. #include "xdr3.h"
  35. #endif /* CONFIG_NFSD_V3 */
  36. #ifdef CONFIG_NFSD_V4
  37. #include "../internal.h"
  38. #include "acl.h"
  39. #include "idmap.h"
  40. #endif /* CONFIG_NFSD_V4 */
  41. #include "nfsd.h"
  42. #include "vfs.h"
  43. #include "filecache.h"
  44. #include "trace.h"
  45. #define NFSDDBG_FACILITY NFSDDBG_FILEOP
  46. /*
  47. * Called from nfsd_lookup and encode_dirent. Check if we have crossed
  48. * a mount point.
  49. * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
  50. * or nfs_ok having possibly changed *dpp and *expp
  51. */
  52. int
  53. nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
  54. struct svc_export **expp)
  55. {
  56. struct svc_export *exp = *expp, *exp2 = NULL;
  57. struct dentry *dentry = *dpp;
  58. struct path path = {.mnt = mntget(exp->ex_path.mnt),
  59. .dentry = dget(dentry)};
  60. int err = 0;
  61. err = follow_down(&path);
  62. if (err < 0)
  63. goto out;
  64. if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
  65. nfsd_mountpoint(dentry, exp) == 2) {
  66. /* This is only a mountpoint in some other namespace */
  67. path_put(&path);
  68. goto out;
  69. }
  70. exp2 = rqst_exp_get_by_name(rqstp, &path);
  71. if (IS_ERR(exp2)) {
  72. err = PTR_ERR(exp2);
  73. /*
  74. * We normally allow NFS clients to continue
  75. * "underneath" a mountpoint that is not exported.
  76. * The exception is V4ROOT, where no traversal is ever
  77. * allowed without an explicit export of the new
  78. * directory.
  79. */
  80. if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
  81. err = 0;
  82. path_put(&path);
  83. goto out;
  84. }
  85. if (nfsd_v4client(rqstp) ||
  86. (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
  87. /* successfully crossed mount point */
  88. /*
  89. * This is subtle: path.dentry is *not* on path.mnt
  90. * at this point. The only reason we are safe is that
  91. * original mnt is pinned down by exp, so we should
  92. * put path *before* putting exp
  93. */
  94. *dpp = path.dentry;
  95. path.dentry = dentry;
  96. *expp = exp2;
  97. exp2 = exp;
  98. }
  99. path_put(&path);
  100. exp_put(exp2);
  101. out:
  102. return err;
  103. }
  104. static void follow_to_parent(struct path *path)
  105. {
  106. struct dentry *dp;
  107. while (path->dentry == path->mnt->mnt_root && follow_up(path))
  108. ;
  109. dp = dget_parent(path->dentry);
  110. dput(path->dentry);
  111. path->dentry = dp;
  112. }
  113. static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
  114. {
  115. struct svc_export *exp2;
  116. struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
  117. .dentry = dget(dparent)};
  118. follow_to_parent(&path);
  119. exp2 = rqst_exp_parent(rqstp, &path);
  120. if (PTR_ERR(exp2) == -ENOENT) {
  121. *dentryp = dget(dparent);
  122. } else if (IS_ERR(exp2)) {
  123. path_put(&path);
  124. return PTR_ERR(exp2);
  125. } else {
  126. *dentryp = dget(path.dentry);
  127. exp_put(*exp);
  128. *exp = exp2;
  129. }
  130. path_put(&path);
  131. return 0;
  132. }
  133. /*
  134. * For nfsd purposes, we treat V4ROOT exports as though there was an
  135. * export at *every* directory.
  136. * We return:
  137. * '1' if this dentry *must* be an export point,
  138. * '2' if it might be, if there is really a mount here, and
  139. * '0' if there is no chance of an export point here.
  140. */
  141. int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
  142. {
  143. if (!d_inode(dentry))
  144. return 0;
  145. if (exp->ex_flags & NFSEXP_V4ROOT)
  146. return 1;
  147. if (nfsd4_is_junction(dentry))
  148. return 1;
  149. if (d_mountpoint(dentry))
  150. /*
  151. * Might only be a mountpoint in a different namespace,
  152. * but we need to check.
  153. */
  154. return 2;
  155. return 0;
  156. }
  157. __be32
  158. nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
  159. const char *name, unsigned int len,
  160. struct svc_export **exp_ret, struct dentry **dentry_ret)
  161. {
  162. struct svc_export *exp;
  163. struct dentry *dparent;
  164. struct dentry *dentry;
  165. int host_err;
  166. dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
  167. dparent = fhp->fh_dentry;
  168. exp = exp_get(fhp->fh_export);
  169. /* Lookup the name, but don't follow links */
  170. if (isdotent(name, len)) {
  171. if (len==1)
  172. dentry = dget(dparent);
  173. else if (dparent != exp->ex_path.dentry)
  174. dentry = dget_parent(dparent);
  175. else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
  176. dentry = dget(dparent); /* .. == . just like at / */
  177. else {
  178. /* checking mountpoint crossing is very different when stepping up */
  179. host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
  180. if (host_err)
  181. goto out_nfserr;
  182. }
  183. } else {
  184. /*
  185. * In the nfsd4_open() case, this may be held across
  186. * subsequent open and delegation acquisition which may
  187. * need to take the child's i_mutex:
  188. */
  189. fh_lock_nested(fhp, I_MUTEX_PARENT);
  190. dentry = lookup_one_len(name, dparent, len);
  191. host_err = PTR_ERR(dentry);
  192. if (IS_ERR(dentry))
  193. goto out_nfserr;
  194. if (nfsd_mountpoint(dentry, exp)) {
  195. /*
  196. * We don't need the i_mutex after all. It's
  197. * still possible we could open this (regular
  198. * files can be mountpoints too), but the
  199. * i_mutex is just there to prevent renames of
  200. * something that we might be about to delegate,
  201. * and a mountpoint won't be renamed:
  202. */
  203. fh_unlock(fhp);
  204. if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
  205. dput(dentry);
  206. goto out_nfserr;
  207. }
  208. }
  209. }
  210. *dentry_ret = dentry;
  211. *exp_ret = exp;
  212. return 0;
  213. out_nfserr:
  214. exp_put(exp);
  215. return nfserrno(host_err);
  216. }
  217. /*
  218. * Look up one component of a pathname.
  219. * N.B. After this call _both_ fhp and resfh need an fh_put
  220. *
  221. * If the lookup would cross a mountpoint, and the mounted filesystem
  222. * is exported to the client with NFSEXP_NOHIDE, then the lookup is
  223. * accepted as it stands and the mounted directory is
  224. * returned. Otherwise the covered directory is returned.
  225. * NOTE: this mountpoint crossing is not supported properly by all
  226. * clients and is explicitly disallowed for NFSv3
  227. * NeilBrown <neilb@cse.unsw.edu.au>
  228. */
  229. __be32
  230. nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
  231. unsigned int len, struct svc_fh *resfh)
  232. {
  233. struct svc_export *exp;
  234. struct dentry *dentry;
  235. __be32 err;
  236. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  237. if (err)
  238. return err;
  239. err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
  240. if (err)
  241. return err;
  242. err = check_nfsd_access(exp, rqstp);
  243. if (err)
  244. goto out;
  245. /*
  246. * Note: we compose the file handle now, but as the
  247. * dentry may be negative, it may need to be updated.
  248. */
  249. err = fh_compose(resfh, exp, dentry, fhp);
  250. if (!err && d_really_is_negative(dentry))
  251. err = nfserr_noent;
  252. out:
  253. dput(dentry);
  254. exp_put(exp);
  255. return err;
  256. }
  257. /*
  258. * Commit metadata changes to stable storage.
  259. */
  260. static int
  261. commit_inode_metadata(struct inode *inode)
  262. {
  263. const struct export_operations *export_ops = inode->i_sb->s_export_op;
  264. if (export_ops->commit_metadata)
  265. return export_ops->commit_metadata(inode);
  266. return sync_inode_metadata(inode, 1);
  267. }
  268. static int
  269. commit_metadata(struct svc_fh *fhp)
  270. {
  271. struct inode *inode = d_inode(fhp->fh_dentry);
  272. if (!EX_ISSYNC(fhp->fh_export))
  273. return 0;
  274. return commit_inode_metadata(inode);
  275. }
  276. /*
  277. * Go over the attributes and take care of the small differences between
  278. * NFS semantics and what Linux expects.
  279. */
  280. static void
  281. nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
  282. {
  283. /* sanitize the mode change */
  284. if (iap->ia_valid & ATTR_MODE) {
  285. iap->ia_mode &= S_IALLUGO;
  286. iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
  287. }
  288. /* Revoke setuid/setgid on chown */
  289. if (!S_ISDIR(inode->i_mode) &&
  290. ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
  291. iap->ia_valid |= ATTR_KILL_PRIV;
  292. if (iap->ia_valid & ATTR_MODE) {
  293. /* we're setting mode too, just clear the s*id bits */
  294. iap->ia_mode &= ~S_ISUID;
  295. if (iap->ia_mode & S_IXGRP)
  296. iap->ia_mode &= ~S_ISGID;
  297. } else {
  298. /* set ATTR_KILL_* bits and let VFS handle it */
  299. iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
  300. }
  301. }
  302. }
  303. static __be32
  304. nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
  305. struct iattr *iap)
  306. {
  307. struct inode *inode = d_inode(fhp->fh_dentry);
  308. int host_err;
  309. if (iap->ia_size < inode->i_size) {
  310. __be32 err;
  311. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  312. NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
  313. if (err)
  314. return err;
  315. }
  316. host_err = get_write_access(inode);
  317. if (host_err)
  318. goto out_nfserrno;
  319. host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
  320. if (host_err)
  321. goto out_put_write_access;
  322. return 0;
  323. out_put_write_access:
  324. put_write_access(inode);
  325. out_nfserrno:
  326. return nfserrno(host_err);
  327. }
  328. /*
  329. * Set various file attributes. After this call fhp needs an fh_put.
  330. */
  331. __be32
  332. nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
  333. int check_guard, time64_t guardtime)
  334. {
  335. struct dentry *dentry;
  336. struct inode *inode;
  337. int accmode = NFSD_MAY_SATTR;
  338. umode_t ftype = 0;
  339. __be32 err;
  340. int host_err;
  341. bool get_write_count;
  342. bool size_change = (iap->ia_valid & ATTR_SIZE);
  343. if (iap->ia_valid & ATTR_SIZE) {
  344. accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
  345. ftype = S_IFREG;
  346. }
  347. /*
  348. * If utimes(2) and friends are called with times not NULL, we should
  349. * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
  350. * will return EACCES, when the caller's effective UID does not match
  351. * the owner of the file, and the caller is not privileged. In this
  352. * situation, we should return EPERM(notify_change will return this).
  353. */
  354. if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
  355. accmode |= NFSD_MAY_OWNER_OVERRIDE;
  356. if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
  357. accmode |= NFSD_MAY_WRITE;
  358. }
  359. /* Callers that do fh_verify should do the fh_want_write: */
  360. get_write_count = !fhp->fh_dentry;
  361. /* Get inode */
  362. err = fh_verify(rqstp, fhp, ftype, accmode);
  363. if (err)
  364. return err;
  365. if (get_write_count) {
  366. host_err = fh_want_write(fhp);
  367. if (host_err)
  368. goto out;
  369. }
  370. dentry = fhp->fh_dentry;
  371. inode = d_inode(dentry);
  372. /* Ignore any mode updates on symlinks */
  373. if (S_ISLNK(inode->i_mode))
  374. iap->ia_valid &= ~ATTR_MODE;
  375. if (!iap->ia_valid)
  376. return 0;
  377. nfsd_sanitize_attrs(inode, iap);
  378. if (check_guard && guardtime != inode->i_ctime.tv_sec)
  379. return nfserr_notsync;
  380. /*
  381. * The size case is special, it changes the file in addition to the
  382. * attributes, and file systems don't expect it to be mixed with
  383. * "random" attribute changes. We thus split out the size change
  384. * into a separate call to ->setattr, and do the rest as a separate
  385. * setattr call.
  386. */
  387. if (size_change) {
  388. err = nfsd_get_write_access(rqstp, fhp, iap);
  389. if (err)
  390. return err;
  391. }
  392. fh_lock(fhp);
  393. if (size_change) {
  394. /*
  395. * RFC5661, Section 18.30.4:
  396. * Changing the size of a file with SETATTR indirectly
  397. * changes the time_modify and change attributes.
  398. *
  399. * (and similar for the older RFCs)
  400. */
  401. struct iattr size_attr = {
  402. .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
  403. .ia_size = iap->ia_size,
  404. };
  405. host_err = notify_change(dentry, &size_attr, NULL);
  406. if (host_err)
  407. goto out_unlock;
  408. iap->ia_valid &= ~ATTR_SIZE;
  409. /*
  410. * Avoid the additional setattr call below if the only other
  411. * attribute that the client sends is the mtime, as we update
  412. * it as part of the size change above.
  413. */
  414. if ((iap->ia_valid & ~ATTR_MTIME) == 0)
  415. goto out_unlock;
  416. }
  417. iap->ia_valid |= ATTR_CTIME;
  418. host_err = notify_change(dentry, iap, NULL);
  419. out_unlock:
  420. fh_unlock(fhp);
  421. if (size_change)
  422. put_write_access(inode);
  423. out:
  424. if (!host_err)
  425. host_err = commit_metadata(fhp);
  426. return nfserrno(host_err);
  427. }
  428. #if defined(CONFIG_NFSD_V4)
  429. /*
  430. * NFS junction information is stored in an extended attribute.
  431. */
  432. #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
  433. /**
  434. * nfsd4_is_junction - Test if an object could be an NFS junction
  435. *
  436. * @dentry: object to test
  437. *
  438. * Returns 1 if "dentry" appears to contain NFS junction information.
  439. * Otherwise 0 is returned.
  440. */
  441. int nfsd4_is_junction(struct dentry *dentry)
  442. {
  443. struct inode *inode = d_inode(dentry);
  444. if (inode == NULL)
  445. return 0;
  446. if (inode->i_mode & S_IXUGO)
  447. return 0;
  448. if (!(inode->i_mode & S_ISVTX))
  449. return 0;
  450. if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
  451. return 0;
  452. return 1;
  453. }
  454. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  455. __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
  456. struct xdr_netobj *label)
  457. {
  458. __be32 error;
  459. int host_error;
  460. struct dentry *dentry;
  461. error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
  462. if (error)
  463. return error;
  464. dentry = fhp->fh_dentry;
  465. inode_lock(d_inode(dentry));
  466. host_error = security_inode_setsecctx(dentry, label->data, label->len);
  467. inode_unlock(d_inode(dentry));
  468. return nfserrno(host_error);
  469. }
  470. #else
  471. __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
  472. struct xdr_netobj *label)
  473. {
  474. return nfserr_notsupp;
  475. }
  476. #endif
  477. __be32 nfsd4_clone_file_range(struct nfsd_file *nf_src, u64 src_pos,
  478. struct nfsd_file *nf_dst, u64 dst_pos, u64 count, bool sync)
  479. {
  480. struct file *src = nf_src->nf_file;
  481. struct file *dst = nf_dst->nf_file;
  482. loff_t cloned;
  483. __be32 ret = 0;
  484. down_write(&nf_dst->nf_rwsem);
  485. cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
  486. if (cloned < 0) {
  487. ret = nfserrno(cloned);
  488. goto out_err;
  489. }
  490. if (count && cloned != count) {
  491. ret = nfserrno(-EINVAL);
  492. goto out_err;
  493. }
  494. if (sync) {
  495. loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
  496. int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
  497. if (!status)
  498. status = commit_inode_metadata(file_inode(src));
  499. if (status < 0) {
  500. nfsd_reset_boot_verifier(net_generic(nf_dst->nf_net,
  501. nfsd_net_id));
  502. ret = nfserrno(status);
  503. }
  504. }
  505. out_err:
  506. up_write(&nf_dst->nf_rwsem);
  507. return ret;
  508. }
  509. ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
  510. u64 dst_pos, u64 count)
  511. {
  512. /*
  513. * Limit copy to 4MB to prevent indefinitely blocking an nfsd
  514. * thread and client rpc slot. The choice of 4MB is somewhat
  515. * arbitrary. We might instead base this on r/wsize, or make it
  516. * tunable, or use a time instead of a byte limit, or implement
  517. * asynchronous copy. In theory a client could also recognize a
  518. * limit like this and pipeline multiple COPY requests.
  519. */
  520. count = min_t(u64, count, 1 << 22);
  521. return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
  522. }
  523. __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
  524. struct file *file, loff_t offset, loff_t len,
  525. int flags)
  526. {
  527. int error;
  528. if (!S_ISREG(file_inode(file)->i_mode))
  529. return nfserr_inval;
  530. error = vfs_fallocate(file, flags, offset, len);
  531. if (!error)
  532. error = commit_metadata(fhp);
  533. return nfserrno(error);
  534. }
  535. #endif /* defined(CONFIG_NFSD_V4) */
  536. #ifdef CONFIG_NFSD_V3
  537. /*
  538. * Check server access rights to a file system object
  539. */
  540. struct accessmap {
  541. u32 access;
  542. int how;
  543. };
  544. static struct accessmap nfs3_regaccess[] = {
  545. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  546. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  547. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
  548. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
  549. #ifdef CONFIG_NFSD_V4
  550. { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
  551. { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
  552. { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
  553. #endif
  554. { 0, 0 }
  555. };
  556. static struct accessmap nfs3_diraccess[] = {
  557. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  558. { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
  559. { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
  560. { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
  561. { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
  562. #ifdef CONFIG_NFSD_V4
  563. { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
  564. { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
  565. { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
  566. #endif
  567. { 0, 0 }
  568. };
  569. static struct accessmap nfs3_anyaccess[] = {
  570. /* Some clients - Solaris 2.6 at least, make an access call
  571. * to the server to check for access for things like /dev/null
  572. * (which really, the server doesn't care about). So
  573. * We provide simple access checking for them, looking
  574. * mainly at mode bits, and we make sure to ignore read-only
  575. * filesystem checks
  576. */
  577. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  578. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  579. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  580. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  581. { 0, 0 }
  582. };
  583. __be32
  584. nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
  585. {
  586. struct accessmap *map;
  587. struct svc_export *export;
  588. struct dentry *dentry;
  589. u32 query, result = 0, sresult = 0;
  590. __be32 error;
  591. error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
  592. if (error)
  593. goto out;
  594. export = fhp->fh_export;
  595. dentry = fhp->fh_dentry;
  596. if (d_is_reg(dentry))
  597. map = nfs3_regaccess;
  598. else if (d_is_dir(dentry))
  599. map = nfs3_diraccess;
  600. else
  601. map = nfs3_anyaccess;
  602. query = *access;
  603. for (; map->access; map++) {
  604. if (map->access & query) {
  605. __be32 err2;
  606. sresult |= map->access;
  607. err2 = nfsd_permission(rqstp, export, dentry, map->how);
  608. switch (err2) {
  609. case nfs_ok:
  610. result |= map->access;
  611. break;
  612. /* the following error codes just mean the access was not allowed,
  613. * rather than an error occurred */
  614. case nfserr_rofs:
  615. case nfserr_acces:
  616. case nfserr_perm:
  617. /* simply don't "or" in the access bit. */
  618. break;
  619. default:
  620. error = err2;
  621. goto out;
  622. }
  623. }
  624. }
  625. *access = result;
  626. if (supported)
  627. *supported = sresult;
  628. out:
  629. return error;
  630. }
  631. #endif /* CONFIG_NFSD_V3 */
  632. int nfsd_open_break_lease(struct inode *inode, int access)
  633. {
  634. unsigned int mode;
  635. if (access & NFSD_MAY_NOT_BREAK_LEASE)
  636. return 0;
  637. mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
  638. return break_lease(inode, mode | O_NONBLOCK);
  639. }
  640. /*
  641. * Open an existing file or directory.
  642. * The may_flags argument indicates the type of open (read/write/lock)
  643. * and additional flags.
  644. * N.B. After this call fhp needs an fh_put
  645. */
  646. static __be32
  647. __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
  648. int may_flags, struct file **filp)
  649. {
  650. struct path path;
  651. struct inode *inode;
  652. struct file *file;
  653. int flags = O_RDONLY|O_LARGEFILE;
  654. __be32 err;
  655. int host_err = 0;
  656. path.mnt = fhp->fh_export->ex_path.mnt;
  657. path.dentry = fhp->fh_dentry;
  658. inode = d_inode(path.dentry);
  659. /* Disallow write access to files with the append-only bit set
  660. * or any access when mandatory locking enabled
  661. */
  662. err = nfserr_perm;
  663. if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
  664. goto out;
  665. /*
  666. * We must ignore files (but only files) which might have mandatory
  667. * locks on them because there is no way to know if the accesser has
  668. * the lock.
  669. */
  670. if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
  671. goto out;
  672. if (!inode->i_fop)
  673. goto out;
  674. host_err = nfsd_open_break_lease(inode, may_flags);
  675. if (host_err) /* NOMEM or WOULDBLOCK */
  676. goto out_nfserr;
  677. if (may_flags & NFSD_MAY_WRITE) {
  678. if (may_flags & NFSD_MAY_READ)
  679. flags = O_RDWR|O_LARGEFILE;
  680. else
  681. flags = O_WRONLY|O_LARGEFILE;
  682. }
  683. file = dentry_open(&path, flags, current_cred());
  684. if (IS_ERR(file)) {
  685. host_err = PTR_ERR(file);
  686. goto out_nfserr;
  687. }
  688. host_err = ima_file_check(file, may_flags);
  689. if (host_err) {
  690. fput(file);
  691. goto out_nfserr;
  692. }
  693. if (may_flags & NFSD_MAY_64BIT_COOKIE)
  694. file->f_mode |= FMODE_64BITHASH;
  695. else
  696. file->f_mode |= FMODE_32BITHASH;
  697. *filp = file;
  698. out_nfserr:
  699. err = nfserrno(host_err);
  700. out:
  701. return err;
  702. }
  703. __be32
  704. nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
  705. int may_flags, struct file **filp)
  706. {
  707. __be32 err;
  708. validate_process_creds();
  709. /*
  710. * If we get here, then the client has already done an "open",
  711. * and (hopefully) checked permission - so allow OWNER_OVERRIDE
  712. * in case a chmod has now revoked permission.
  713. *
  714. * Arguably we should also allow the owner override for
  715. * directories, but we never have and it doesn't seem to have
  716. * caused anyone a problem. If we were to change this, note
  717. * also that our filldir callbacks would need a variant of
  718. * lookup_one_len that doesn't check permissions.
  719. */
  720. if (type == S_IFREG)
  721. may_flags |= NFSD_MAY_OWNER_OVERRIDE;
  722. err = fh_verify(rqstp, fhp, type, may_flags);
  723. if (!err)
  724. err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
  725. validate_process_creds();
  726. return err;
  727. }
  728. __be32
  729. nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
  730. int may_flags, struct file **filp)
  731. {
  732. __be32 err;
  733. validate_process_creds();
  734. err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
  735. validate_process_creds();
  736. return err;
  737. }
  738. /*
  739. * Grab and keep cached pages associated with a file in the svc_rqst
  740. * so that they can be passed to the network sendmsg/sendpage routines
  741. * directly. They will be released after the sending has completed.
  742. */
  743. static int
  744. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  745. struct splice_desc *sd)
  746. {
  747. struct svc_rqst *rqstp = sd->u.data;
  748. struct page **pp = rqstp->rq_next_page;
  749. struct page *page = buf->page;
  750. size_t size;
  751. size = sd->len;
  752. if (rqstp->rq_res.page_len == 0) {
  753. get_page(page);
  754. put_page(*rqstp->rq_next_page);
  755. *(rqstp->rq_next_page++) = page;
  756. rqstp->rq_res.page_base = buf->offset;
  757. rqstp->rq_res.page_len = size;
  758. } else if (page != pp[-1]) {
  759. get_page(page);
  760. if (*rqstp->rq_next_page)
  761. put_page(*rqstp->rq_next_page);
  762. *(rqstp->rq_next_page++) = page;
  763. rqstp->rq_res.page_len += size;
  764. } else
  765. rqstp->rq_res.page_len += size;
  766. return size;
  767. }
  768. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  769. struct splice_desc *sd)
  770. {
  771. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  772. }
  773. static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
  774. size_t expected)
  775. {
  776. if (expected != 0 && len == 0)
  777. return 1;
  778. if (offset+len >= i_size_read(file_inode(file)))
  779. return 1;
  780. return 0;
  781. }
  782. static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  783. struct file *file, loff_t offset,
  784. unsigned long *count, u32 *eof, ssize_t host_err)
  785. {
  786. if (host_err >= 0) {
  787. nfsdstats.io_read += host_err;
  788. *eof = nfsd_eof_on_read(file, offset, host_err, *count);
  789. *count = host_err;
  790. fsnotify_access(file);
  791. trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
  792. return 0;
  793. } else {
  794. trace_nfsd_read_err(rqstp, fhp, offset, host_err);
  795. return nfserrno(host_err);
  796. }
  797. }
  798. __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  799. struct file *file, loff_t offset, unsigned long *count,
  800. u32 *eof)
  801. {
  802. struct splice_desc sd = {
  803. .len = 0,
  804. .total_len = *count,
  805. .pos = offset,
  806. .u.data = rqstp,
  807. };
  808. ssize_t host_err;
  809. trace_nfsd_read_splice(rqstp, fhp, offset, *count);
  810. rqstp->rq_next_page = rqstp->rq_respages + 1;
  811. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  812. return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
  813. }
  814. __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
  815. struct file *file, loff_t offset,
  816. struct kvec *vec, int vlen, unsigned long *count,
  817. u32 *eof)
  818. {
  819. struct iov_iter iter;
  820. loff_t ppos = offset;
  821. ssize_t host_err;
  822. trace_nfsd_read_vector(rqstp, fhp, offset, *count);
  823. iov_iter_kvec(&iter, READ, vec, vlen, *count);
  824. host_err = vfs_iter_read(file, &iter, &ppos, 0);
  825. return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
  826. }
  827. /*
  828. * Gathered writes: If another process is currently writing to the file,
  829. * there's a high chance this is another nfsd (triggered by a bulk write
  830. * from a client's biod). Rather than syncing the file with each write
  831. * request, we sleep for 10 msec.
  832. *
  833. * I don't know if this roughly approximates C. Juszak's idea of
  834. * gathered writes, but it's a nice and simple solution (IMHO), and it
  835. * seems to work:-)
  836. *
  837. * Note: we do this only in the NFSv2 case, since v3 and higher have a
  838. * better tool (separate unstable writes and commits) for solving this
  839. * problem.
  840. */
  841. static int wait_for_concurrent_writes(struct file *file)
  842. {
  843. struct inode *inode = file_inode(file);
  844. static ino_t last_ino;
  845. static dev_t last_dev;
  846. int err = 0;
  847. if (atomic_read(&inode->i_writecount) > 1
  848. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  849. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  850. msleep(10);
  851. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  852. }
  853. if (inode->i_state & I_DIRTY) {
  854. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  855. err = vfs_fsync(file, 0);
  856. }
  857. last_ino = inode->i_ino;
  858. last_dev = inode->i_sb->s_dev;
  859. return err;
  860. }
  861. __be32
  862. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
  863. loff_t offset, struct kvec *vec, int vlen,
  864. unsigned long *cnt, int stable,
  865. __be32 *verf)
  866. {
  867. struct file *file = nf->nf_file;
  868. struct svc_export *exp;
  869. struct iov_iter iter;
  870. __be32 nfserr;
  871. int host_err;
  872. int use_wgather;
  873. loff_t pos = offset;
  874. unsigned int pflags = current->flags;
  875. rwf_t flags = 0;
  876. trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
  877. if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
  878. /*
  879. * We want throttling in balance_dirty_pages()
  880. * and shrink_inactive_list() to only consider
  881. * the backingdev we are writing to, so that nfs to
  882. * localhost doesn't cause nfsd to lock up due to all
  883. * the client's dirty pages or its congested queue.
  884. */
  885. current->flags |= PF_LOCAL_THROTTLE;
  886. exp = fhp->fh_export;
  887. use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
  888. if (!EX_ISSYNC(exp))
  889. stable = NFS_UNSTABLE;
  890. if (stable && !use_wgather)
  891. flags |= RWF_SYNC;
  892. iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
  893. if (flags & RWF_SYNC) {
  894. down_write(&nf->nf_rwsem);
  895. host_err = vfs_iter_write(file, &iter, &pos, flags);
  896. if (host_err < 0)
  897. nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
  898. nfsd_net_id));
  899. up_write(&nf->nf_rwsem);
  900. } else {
  901. down_read(&nf->nf_rwsem);
  902. if (verf)
  903. nfsd_copy_boot_verifier(verf,
  904. net_generic(SVC_NET(rqstp),
  905. nfsd_net_id));
  906. host_err = vfs_iter_write(file, &iter, &pos, flags);
  907. up_read(&nf->nf_rwsem);
  908. }
  909. if (host_err < 0) {
  910. nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
  911. nfsd_net_id));
  912. goto out_nfserr;
  913. }
  914. *cnt = host_err;
  915. nfsdstats.io_write += *cnt;
  916. fsnotify_modify(file);
  917. if (stable && use_wgather) {
  918. host_err = wait_for_concurrent_writes(file);
  919. if (host_err < 0)
  920. nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
  921. nfsd_net_id));
  922. }
  923. out_nfserr:
  924. if (host_err >= 0) {
  925. trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
  926. nfserr = nfs_ok;
  927. } else {
  928. trace_nfsd_write_err(rqstp, fhp, offset, host_err);
  929. nfserr = nfserrno(host_err);
  930. }
  931. if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
  932. current_restore_flags(pflags, PF_LOCAL_THROTTLE);
  933. return nfserr;
  934. }
  935. /*
  936. * Read data from a file. count must contain the requested read count
  937. * on entry. On return, *count contains the number of bytes actually read.
  938. * N.B. After this call fhp needs an fh_put
  939. */
  940. __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  941. loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
  942. u32 *eof)
  943. {
  944. struct nfsd_file *nf;
  945. struct file *file;
  946. __be32 err;
  947. trace_nfsd_read_start(rqstp, fhp, offset, *count);
  948. err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
  949. if (err)
  950. return err;
  951. file = nf->nf_file;
  952. if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
  953. err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
  954. else
  955. err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
  956. nfsd_file_put(nf);
  957. trace_nfsd_read_done(rqstp, fhp, offset, *count);
  958. return err;
  959. }
  960. /*
  961. * Write data to a file.
  962. * The stable flag requests synchronous writes.
  963. * N.B. After this call fhp needs an fh_put
  964. */
  965. __be32
  966. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
  967. struct kvec *vec, int vlen, unsigned long *cnt, int stable,
  968. __be32 *verf)
  969. {
  970. struct nfsd_file *nf;
  971. __be32 err;
  972. trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
  973. err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
  974. if (err)
  975. goto out;
  976. err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
  977. vlen, cnt, stable, verf);
  978. nfsd_file_put(nf);
  979. out:
  980. trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
  981. return err;
  982. }
  983. #ifdef CONFIG_NFSD_V3
  984. static int
  985. nfsd_filemap_write_and_wait_range(struct nfsd_file *nf, loff_t offset,
  986. loff_t end)
  987. {
  988. struct address_space *mapping = nf->nf_file->f_mapping;
  989. int ret = filemap_fdatawrite_range(mapping, offset, end);
  990. if (ret)
  991. return ret;
  992. filemap_fdatawait_range_keep_errors(mapping, offset, end);
  993. return 0;
  994. }
  995. /*
  996. * Commit all pending writes to stable storage.
  997. *
  998. * Note: we only guarantee that data that lies within the range specified
  999. * by the 'offset' and 'count' parameters will be synced.
  1000. *
  1001. * Unfortunately we cannot lock the file to make sure we return full WCC
  1002. * data to the client, as locking happens lower down in the filesystem.
  1003. */
  1004. __be32
  1005. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1006. loff_t offset, unsigned long count, __be32 *verf)
  1007. {
  1008. struct nfsd_file *nf;
  1009. loff_t end = LLONG_MAX;
  1010. __be32 err = nfserr_inval;
  1011. if (offset < 0)
  1012. goto out;
  1013. if (count != 0) {
  1014. end = offset + (loff_t)count - 1;
  1015. if (end < offset)
  1016. goto out;
  1017. }
  1018. err = nfsd_file_acquire(rqstp, fhp,
  1019. NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf);
  1020. if (err)
  1021. goto out;
  1022. if (EX_ISSYNC(fhp->fh_export)) {
  1023. int err2 = nfsd_filemap_write_and_wait_range(nf, offset, end);
  1024. down_write(&nf->nf_rwsem);
  1025. if (!err2)
  1026. err2 = vfs_fsync_range(nf->nf_file, offset, end, 0);
  1027. switch (err2) {
  1028. case 0:
  1029. nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net,
  1030. nfsd_net_id));
  1031. break;
  1032. case -EINVAL:
  1033. err = nfserr_notsupp;
  1034. break;
  1035. default:
  1036. err = nfserrno(err2);
  1037. nfsd_reset_boot_verifier(net_generic(nf->nf_net,
  1038. nfsd_net_id));
  1039. }
  1040. up_write(&nf->nf_rwsem);
  1041. } else
  1042. nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net,
  1043. nfsd_net_id));
  1044. nfsd_file_put(nf);
  1045. out:
  1046. return err;
  1047. }
  1048. #endif /* CONFIG_NFSD_V3 */
  1049. static __be32
  1050. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
  1051. struct iattr *iap)
  1052. {
  1053. /*
  1054. * Mode has already been set earlier in create:
  1055. */
  1056. iap->ia_valid &= ~ATTR_MODE;
  1057. /*
  1058. * Setting uid/gid works only for root. Irix appears to
  1059. * send along the gid on create when it tries to implement
  1060. * setgid directories via NFS:
  1061. */
  1062. if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  1063. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  1064. if (iap->ia_valid)
  1065. return nfsd_setattr(rqstp, resfhp, iap, 0, (time64_t)0);
  1066. /* Callers expect file metadata to be committed here */
  1067. return nfserrno(commit_metadata(resfhp));
  1068. }
  1069. /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
  1070. * setting size to 0 may fail for some specific file systems by the permission
  1071. * checking which requires WRITE permission but the mode is 000.
  1072. * we ignore the resizing(to 0) on the just new created file, since the size is
  1073. * 0 after file created.
  1074. *
  1075. * call this only after vfs_create() is called.
  1076. * */
  1077. static void
  1078. nfsd_check_ignore_resizing(struct iattr *iap)
  1079. {
  1080. if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
  1081. iap->ia_valid &= ~ATTR_SIZE;
  1082. }
  1083. /* The parent directory should already be locked: */
  1084. __be32
  1085. nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1086. char *fname, int flen, struct iattr *iap,
  1087. int type, dev_t rdev, struct svc_fh *resfhp)
  1088. {
  1089. struct dentry *dentry, *dchild;
  1090. struct inode *dirp;
  1091. __be32 err;
  1092. __be32 err2;
  1093. int host_err;
  1094. dentry = fhp->fh_dentry;
  1095. dirp = d_inode(dentry);
  1096. dchild = dget(resfhp->fh_dentry);
  1097. if (!fhp->fh_locked) {
  1098. WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
  1099. dentry);
  1100. err = nfserr_io;
  1101. goto out;
  1102. }
  1103. err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
  1104. if (err)
  1105. goto out;
  1106. if (!(iap->ia_valid & ATTR_MODE))
  1107. iap->ia_mode = 0;
  1108. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1109. if (!IS_POSIXACL(dirp))
  1110. iap->ia_mode &= ~current_umask();
  1111. err = 0;
  1112. host_err = 0;
  1113. switch (type) {
  1114. case S_IFREG:
  1115. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1116. if (!host_err)
  1117. nfsd_check_ignore_resizing(iap);
  1118. break;
  1119. case S_IFDIR:
  1120. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1121. if (!host_err && unlikely(d_unhashed(dchild))) {
  1122. struct dentry *d;
  1123. d = lookup_one_len(dchild->d_name.name,
  1124. dchild->d_parent,
  1125. dchild->d_name.len);
  1126. if (IS_ERR(d)) {
  1127. host_err = PTR_ERR(d);
  1128. break;
  1129. }
  1130. if (unlikely(d_is_negative(d))) {
  1131. dput(d);
  1132. err = nfserr_serverfault;
  1133. goto out;
  1134. }
  1135. dput(resfhp->fh_dentry);
  1136. resfhp->fh_dentry = dget(d);
  1137. err = fh_update(resfhp);
  1138. dput(dchild);
  1139. dchild = d;
  1140. if (err)
  1141. goto out;
  1142. }
  1143. break;
  1144. case S_IFCHR:
  1145. case S_IFBLK:
  1146. case S_IFIFO:
  1147. case S_IFSOCK:
  1148. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1149. break;
  1150. default:
  1151. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1152. type);
  1153. host_err = -EINVAL;
  1154. }
  1155. if (host_err < 0)
  1156. goto out_nfserr;
  1157. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1158. /*
  1159. * nfsd_create_setattr already committed the child. Transactional
  1160. * filesystems had a chance to commit changes for both parent and
  1161. * child simultaneously making the following commit_metadata a
  1162. * noop.
  1163. */
  1164. err2 = nfserrno(commit_metadata(fhp));
  1165. if (err2)
  1166. err = err2;
  1167. /*
  1168. * Update the file handle to get the new inode info.
  1169. */
  1170. if (!err)
  1171. err = fh_update(resfhp);
  1172. out:
  1173. dput(dchild);
  1174. return err;
  1175. out_nfserr:
  1176. err = nfserrno(host_err);
  1177. goto out;
  1178. }
  1179. /*
  1180. * Create a filesystem object (regular, directory, special).
  1181. * Note that the parent directory is left locked.
  1182. *
  1183. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1184. */
  1185. __be32
  1186. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1187. char *fname, int flen, struct iattr *iap,
  1188. int type, dev_t rdev, struct svc_fh *resfhp)
  1189. {
  1190. struct dentry *dentry, *dchild = NULL;
  1191. __be32 err;
  1192. int host_err;
  1193. if (isdotent(fname, flen))
  1194. return nfserr_exist;
  1195. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
  1196. if (err)
  1197. return err;
  1198. dentry = fhp->fh_dentry;
  1199. host_err = fh_want_write(fhp);
  1200. if (host_err)
  1201. return nfserrno(host_err);
  1202. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1203. dchild = lookup_one_len(fname, dentry, flen);
  1204. host_err = PTR_ERR(dchild);
  1205. if (IS_ERR(dchild))
  1206. return nfserrno(host_err);
  1207. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1208. /*
  1209. * We unconditionally drop our ref to dchild as fh_compose will have
  1210. * already grabbed its own ref for it.
  1211. */
  1212. dput(dchild);
  1213. if (err)
  1214. return err;
  1215. return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
  1216. rdev, resfhp);
  1217. }
  1218. #ifdef CONFIG_NFSD_V3
  1219. /*
  1220. * NFSv3 and NFSv4 version of nfsd_create
  1221. */
  1222. __be32
  1223. do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1224. char *fname, int flen, struct iattr *iap,
  1225. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1226. bool *truncp, bool *created)
  1227. {
  1228. struct dentry *dentry, *dchild = NULL;
  1229. struct inode *dirp;
  1230. __be32 err;
  1231. int host_err;
  1232. __u32 v_mtime=0, v_atime=0;
  1233. err = nfserr_perm;
  1234. if (!flen)
  1235. goto out;
  1236. err = nfserr_exist;
  1237. if (isdotent(fname, flen))
  1238. goto out;
  1239. if (!(iap->ia_valid & ATTR_MODE))
  1240. iap->ia_mode = 0;
  1241. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  1242. if (err)
  1243. goto out;
  1244. dentry = fhp->fh_dentry;
  1245. dirp = d_inode(dentry);
  1246. host_err = fh_want_write(fhp);
  1247. if (host_err)
  1248. goto out_nfserr;
  1249. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1250. /*
  1251. * Compose the response file handle.
  1252. */
  1253. dchild = lookup_one_len(fname, dentry, flen);
  1254. host_err = PTR_ERR(dchild);
  1255. if (IS_ERR(dchild))
  1256. goto out_nfserr;
  1257. /* If file doesn't exist, check for permissions to create one */
  1258. if (d_really_is_negative(dchild)) {
  1259. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1260. if (err)
  1261. goto out;
  1262. }
  1263. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1264. if (err)
  1265. goto out;
  1266. if (nfsd_create_is_exclusive(createmode)) {
  1267. /* solaris7 gets confused (bugid 4218508) if these have
  1268. * the high bit set, so just clear the high bits. If this is
  1269. * ever changed to use different attrs for storing the
  1270. * verifier, then do_open_lookup() will also need to be fixed
  1271. * accordingly.
  1272. */
  1273. v_mtime = verifier[0]&0x7fffffff;
  1274. v_atime = verifier[1]&0x7fffffff;
  1275. }
  1276. if (d_really_is_positive(dchild)) {
  1277. err = 0;
  1278. switch (createmode) {
  1279. case NFS3_CREATE_UNCHECKED:
  1280. if (! d_is_reg(dchild))
  1281. goto out;
  1282. else if (truncp) {
  1283. /* in nfsv4, we need to treat this case a little
  1284. * differently. we don't want to truncate the
  1285. * file now; this would be wrong if the OPEN
  1286. * fails for some other reason. furthermore,
  1287. * if the size is nonzero, we should ignore it
  1288. * according to spec!
  1289. */
  1290. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1291. }
  1292. else {
  1293. iap->ia_valid &= ATTR_SIZE;
  1294. goto set_attr;
  1295. }
  1296. break;
  1297. case NFS3_CREATE_EXCLUSIVE:
  1298. if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
  1299. && d_inode(dchild)->i_atime.tv_sec == v_atime
  1300. && d_inode(dchild)->i_size == 0 ) {
  1301. if (created)
  1302. *created = true;
  1303. break;
  1304. }
  1305. fallthrough;
  1306. case NFS4_CREATE_EXCLUSIVE4_1:
  1307. if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
  1308. && d_inode(dchild)->i_atime.tv_sec == v_atime
  1309. && d_inode(dchild)->i_size == 0 ) {
  1310. if (created)
  1311. *created = true;
  1312. goto set_attr;
  1313. }
  1314. fallthrough;
  1315. case NFS3_CREATE_GUARDED:
  1316. err = nfserr_exist;
  1317. }
  1318. fh_drop_write(fhp);
  1319. goto out;
  1320. }
  1321. if (!IS_POSIXACL(dirp))
  1322. iap->ia_mode &= ~current_umask();
  1323. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1324. if (host_err < 0) {
  1325. fh_drop_write(fhp);
  1326. goto out_nfserr;
  1327. }
  1328. if (created)
  1329. *created = true;
  1330. nfsd_check_ignore_resizing(iap);
  1331. if (nfsd_create_is_exclusive(createmode)) {
  1332. /* Cram the verifier into atime/mtime */
  1333. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1334. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1335. /* XXX someone who knows this better please fix it for nsec */
  1336. iap->ia_mtime.tv_sec = v_mtime;
  1337. iap->ia_atime.tv_sec = v_atime;
  1338. iap->ia_mtime.tv_nsec = 0;
  1339. iap->ia_atime.tv_nsec = 0;
  1340. }
  1341. set_attr:
  1342. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1343. /*
  1344. * nfsd_create_setattr already committed the child
  1345. * (and possibly also the parent).
  1346. */
  1347. if (!err)
  1348. err = nfserrno(commit_metadata(fhp));
  1349. /*
  1350. * Update the filehandle to get the new inode info.
  1351. */
  1352. if (!err)
  1353. err = fh_update(resfhp);
  1354. out:
  1355. fh_unlock(fhp);
  1356. if (dchild && !IS_ERR(dchild))
  1357. dput(dchild);
  1358. fh_drop_write(fhp);
  1359. return err;
  1360. out_nfserr:
  1361. err = nfserrno(host_err);
  1362. goto out;
  1363. }
  1364. #endif /* CONFIG_NFSD_V3 */
  1365. /*
  1366. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1367. * fits into the buffer. On return, it contains the true length.
  1368. * N.B. After this call fhp needs an fh_put
  1369. */
  1370. __be32
  1371. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1372. {
  1373. __be32 err;
  1374. const char *link;
  1375. struct path path;
  1376. DEFINE_DELAYED_CALL(done);
  1377. int len;
  1378. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1379. if (unlikely(err))
  1380. return err;
  1381. path.mnt = fhp->fh_export->ex_path.mnt;
  1382. path.dentry = fhp->fh_dentry;
  1383. if (unlikely(!d_is_symlink(path.dentry)))
  1384. return nfserr_inval;
  1385. touch_atime(&path);
  1386. link = vfs_get_link(path.dentry, &done);
  1387. if (IS_ERR(link))
  1388. return nfserrno(PTR_ERR(link));
  1389. len = strlen(link);
  1390. if (len < *lenp)
  1391. *lenp = len;
  1392. memcpy(buf, link, *lenp);
  1393. do_delayed_call(&done);
  1394. return 0;
  1395. }
  1396. /*
  1397. * Create a symlink and look up its inode
  1398. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1399. */
  1400. __be32
  1401. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1402. char *fname, int flen,
  1403. char *path,
  1404. struct svc_fh *resfhp)
  1405. {
  1406. struct dentry *dentry, *dnew;
  1407. __be32 err, cerr;
  1408. int host_err;
  1409. err = nfserr_noent;
  1410. if (!flen || path[0] == '\0')
  1411. goto out;
  1412. err = nfserr_exist;
  1413. if (isdotent(fname, flen))
  1414. goto out;
  1415. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1416. if (err)
  1417. goto out;
  1418. host_err = fh_want_write(fhp);
  1419. if (host_err)
  1420. goto out_nfserr;
  1421. fh_lock(fhp);
  1422. dentry = fhp->fh_dentry;
  1423. dnew = lookup_one_len(fname, dentry, flen);
  1424. host_err = PTR_ERR(dnew);
  1425. if (IS_ERR(dnew))
  1426. goto out_nfserr;
  1427. host_err = vfs_symlink(d_inode(dentry), dnew, path);
  1428. err = nfserrno(host_err);
  1429. if (!err)
  1430. err = nfserrno(commit_metadata(fhp));
  1431. fh_unlock(fhp);
  1432. fh_drop_write(fhp);
  1433. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1434. dput(dnew);
  1435. if (err==0) err = cerr;
  1436. out:
  1437. return err;
  1438. out_nfserr:
  1439. err = nfserrno(host_err);
  1440. goto out;
  1441. }
  1442. /*
  1443. * Create a hardlink
  1444. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1445. */
  1446. __be32
  1447. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1448. char *name, int len, struct svc_fh *tfhp)
  1449. {
  1450. struct dentry *ddir, *dnew, *dold;
  1451. struct inode *dirp;
  1452. __be32 err;
  1453. int host_err;
  1454. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1455. if (err)
  1456. goto out;
  1457. err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
  1458. if (err)
  1459. goto out;
  1460. err = nfserr_isdir;
  1461. if (d_is_dir(tfhp->fh_dentry))
  1462. goto out;
  1463. err = nfserr_perm;
  1464. if (!len)
  1465. goto out;
  1466. err = nfserr_exist;
  1467. if (isdotent(name, len))
  1468. goto out;
  1469. host_err = fh_want_write(tfhp);
  1470. if (host_err) {
  1471. err = nfserrno(host_err);
  1472. goto out;
  1473. }
  1474. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1475. ddir = ffhp->fh_dentry;
  1476. dirp = d_inode(ddir);
  1477. dnew = lookup_one_len(name, ddir, len);
  1478. host_err = PTR_ERR(dnew);
  1479. if (IS_ERR(dnew))
  1480. goto out_nfserr;
  1481. dold = tfhp->fh_dentry;
  1482. err = nfserr_noent;
  1483. if (d_really_is_negative(dold))
  1484. goto out_dput;
  1485. host_err = vfs_link(dold, dirp, dnew, NULL);
  1486. if (!host_err) {
  1487. err = nfserrno(commit_metadata(ffhp));
  1488. if (!err)
  1489. err = nfserrno(commit_metadata(tfhp));
  1490. } else {
  1491. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1492. err = nfserr_acces;
  1493. else
  1494. err = nfserrno(host_err);
  1495. }
  1496. out_dput:
  1497. dput(dnew);
  1498. out_unlock:
  1499. fh_unlock(ffhp);
  1500. fh_drop_write(tfhp);
  1501. out:
  1502. return err;
  1503. out_nfserr:
  1504. err = nfserrno(host_err);
  1505. goto out_unlock;
  1506. }
  1507. static void
  1508. nfsd_close_cached_files(struct dentry *dentry)
  1509. {
  1510. struct inode *inode = d_inode(dentry);
  1511. if (inode && S_ISREG(inode->i_mode))
  1512. nfsd_file_close_inode_sync(inode);
  1513. }
  1514. static bool
  1515. nfsd_has_cached_files(struct dentry *dentry)
  1516. {
  1517. bool ret = false;
  1518. struct inode *inode = d_inode(dentry);
  1519. if (inode && S_ISREG(inode->i_mode))
  1520. ret = nfsd_file_is_cached(inode);
  1521. return ret;
  1522. }
  1523. /*
  1524. * Rename a file
  1525. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1526. */
  1527. __be32
  1528. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1529. struct svc_fh *tfhp, char *tname, int tlen)
  1530. {
  1531. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1532. struct inode *fdir, *tdir;
  1533. __be32 err;
  1534. int host_err;
  1535. bool has_cached = false;
  1536. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1537. if (err)
  1538. goto out;
  1539. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1540. if (err)
  1541. goto out;
  1542. fdentry = ffhp->fh_dentry;
  1543. fdir = d_inode(fdentry);
  1544. tdentry = tfhp->fh_dentry;
  1545. tdir = d_inode(tdentry);
  1546. err = nfserr_perm;
  1547. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1548. goto out;
  1549. retry:
  1550. host_err = fh_want_write(ffhp);
  1551. if (host_err) {
  1552. err = nfserrno(host_err);
  1553. goto out;
  1554. }
  1555. /* cannot use fh_lock as we need deadlock protective ordering
  1556. * so do it by hand */
  1557. trap = lock_rename(tdentry, fdentry);
  1558. ffhp->fh_locked = tfhp->fh_locked = true;
  1559. fill_pre_wcc(ffhp);
  1560. fill_pre_wcc(tfhp);
  1561. odentry = lookup_one_len(fname, fdentry, flen);
  1562. host_err = PTR_ERR(odentry);
  1563. if (IS_ERR(odentry))
  1564. goto out_nfserr;
  1565. host_err = -ENOENT;
  1566. if (d_really_is_negative(odentry))
  1567. goto out_dput_old;
  1568. host_err = -EINVAL;
  1569. if (odentry == trap)
  1570. goto out_dput_old;
  1571. ndentry = lookup_one_len(tname, tdentry, tlen);
  1572. host_err = PTR_ERR(ndentry);
  1573. if (IS_ERR(ndentry))
  1574. goto out_dput_old;
  1575. host_err = -ENOTEMPTY;
  1576. if (ndentry == trap)
  1577. goto out_dput_new;
  1578. host_err = -EXDEV;
  1579. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1580. goto out_dput_new;
  1581. if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
  1582. goto out_dput_new;
  1583. if (nfsd_has_cached_files(ndentry)) {
  1584. has_cached = true;
  1585. goto out_dput_old;
  1586. } else {
  1587. host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
  1588. if (!host_err) {
  1589. host_err = commit_metadata(tfhp);
  1590. if (!host_err)
  1591. host_err = commit_metadata(ffhp);
  1592. }
  1593. }
  1594. out_dput_new:
  1595. dput(ndentry);
  1596. out_dput_old:
  1597. dput(odentry);
  1598. out_nfserr:
  1599. err = nfserrno(host_err);
  1600. /*
  1601. * We cannot rely on fh_unlock on the two filehandles,
  1602. * as that would do the wrong thing if the two directories
  1603. * were the same, so again we do it by hand.
  1604. */
  1605. if (!has_cached) {
  1606. fill_post_wcc(ffhp);
  1607. fill_post_wcc(tfhp);
  1608. }
  1609. unlock_rename(tdentry, fdentry);
  1610. ffhp->fh_locked = tfhp->fh_locked = false;
  1611. fh_drop_write(ffhp);
  1612. /*
  1613. * If the target dentry has cached open files, then we need to try to
  1614. * close them prior to doing the rename. Flushing delayed fput
  1615. * shouldn't be done with locks held however, so we delay it until this
  1616. * point and then reattempt the whole shebang.
  1617. */
  1618. if (has_cached) {
  1619. has_cached = false;
  1620. nfsd_close_cached_files(ndentry);
  1621. dput(ndentry);
  1622. goto retry;
  1623. }
  1624. out:
  1625. return err;
  1626. }
  1627. /*
  1628. * Unlink a file or directory
  1629. * N.B. After this call fhp needs an fh_put
  1630. */
  1631. __be32
  1632. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1633. char *fname, int flen)
  1634. {
  1635. struct dentry *dentry, *rdentry;
  1636. struct inode *dirp;
  1637. __be32 err;
  1638. int host_err;
  1639. err = nfserr_acces;
  1640. if (!flen || isdotent(fname, flen))
  1641. goto out;
  1642. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1643. if (err)
  1644. goto out;
  1645. host_err = fh_want_write(fhp);
  1646. if (host_err)
  1647. goto out_nfserr;
  1648. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1649. dentry = fhp->fh_dentry;
  1650. dirp = d_inode(dentry);
  1651. rdentry = lookup_one_len(fname, dentry, flen);
  1652. host_err = PTR_ERR(rdentry);
  1653. if (IS_ERR(rdentry))
  1654. goto out_drop_write;
  1655. if (d_really_is_negative(rdentry)) {
  1656. dput(rdentry);
  1657. host_err = -ENOENT;
  1658. goto out_drop_write;
  1659. }
  1660. if (!type)
  1661. type = d_inode(rdentry)->i_mode & S_IFMT;
  1662. if (type != S_IFDIR) {
  1663. nfsd_close_cached_files(rdentry);
  1664. host_err = vfs_unlink(dirp, rdentry, NULL);
  1665. } else {
  1666. host_err = vfs_rmdir(dirp, rdentry);
  1667. }
  1668. if (!host_err)
  1669. host_err = commit_metadata(fhp);
  1670. dput(rdentry);
  1671. out_drop_write:
  1672. fh_drop_write(fhp);
  1673. out_nfserr:
  1674. if (host_err == -EBUSY) {
  1675. /* name is mounted-on. There is no perfect
  1676. * error status.
  1677. */
  1678. if (nfsd_v4client(rqstp))
  1679. err = nfserr_file_open;
  1680. else
  1681. err = nfserr_acces;
  1682. } else {
  1683. err = nfserrno(host_err);
  1684. }
  1685. out:
  1686. return err;
  1687. }
  1688. /*
  1689. * We do this buffering because we must not call back into the file
  1690. * system's ->lookup() method from the filldir callback. That may well
  1691. * deadlock a number of file systems.
  1692. *
  1693. * This is based heavily on the implementation of same in XFS.
  1694. */
  1695. struct buffered_dirent {
  1696. u64 ino;
  1697. loff_t offset;
  1698. int namlen;
  1699. unsigned int d_type;
  1700. char name[];
  1701. };
  1702. struct readdir_data {
  1703. struct dir_context ctx;
  1704. char *dirent;
  1705. size_t used;
  1706. int full;
  1707. };
  1708. static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
  1709. int namlen, loff_t offset, u64 ino,
  1710. unsigned int d_type)
  1711. {
  1712. struct readdir_data *buf =
  1713. container_of(ctx, struct readdir_data, ctx);
  1714. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1715. unsigned int reclen;
  1716. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1717. if (buf->used + reclen > PAGE_SIZE) {
  1718. buf->full = 1;
  1719. return -EINVAL;
  1720. }
  1721. de->namlen = namlen;
  1722. de->offset = offset;
  1723. de->ino = ino;
  1724. de->d_type = d_type;
  1725. memcpy(de->name, name, namlen);
  1726. buf->used += reclen;
  1727. return 0;
  1728. }
  1729. static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
  1730. struct readdir_cd *cdp, loff_t *offsetp)
  1731. {
  1732. struct buffered_dirent *de;
  1733. int host_err;
  1734. int size;
  1735. loff_t offset;
  1736. struct readdir_data buf = {
  1737. .ctx.actor = nfsd_buffered_filldir,
  1738. .dirent = (void *)__get_free_page(GFP_KERNEL)
  1739. };
  1740. if (!buf.dirent)
  1741. return nfserrno(-ENOMEM);
  1742. offset = *offsetp;
  1743. while (1) {
  1744. unsigned int reclen;
  1745. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1746. buf.used = 0;
  1747. buf.full = 0;
  1748. host_err = iterate_dir(file, &buf.ctx);
  1749. if (buf.full)
  1750. host_err = 0;
  1751. if (host_err < 0)
  1752. break;
  1753. size = buf.used;
  1754. if (!size)
  1755. break;
  1756. de = (struct buffered_dirent *)buf.dirent;
  1757. while (size > 0) {
  1758. offset = de->offset;
  1759. if (func(cdp, de->name, de->namlen, de->offset,
  1760. de->ino, de->d_type))
  1761. break;
  1762. if (cdp->err != nfs_ok)
  1763. break;
  1764. reclen = ALIGN(sizeof(*de) + de->namlen,
  1765. sizeof(u64));
  1766. size -= reclen;
  1767. de = (struct buffered_dirent *)((char *)de + reclen);
  1768. }
  1769. if (size > 0) /* We bailed out early */
  1770. break;
  1771. offset = vfs_llseek(file, 0, SEEK_CUR);
  1772. }
  1773. free_page((unsigned long)(buf.dirent));
  1774. if (host_err)
  1775. return nfserrno(host_err);
  1776. *offsetp = offset;
  1777. return cdp->err;
  1778. }
  1779. /*
  1780. * Read entries from a directory.
  1781. * The NFSv3/4 verifier we ignore for now.
  1782. */
  1783. __be32
  1784. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1785. struct readdir_cd *cdp, nfsd_filldir_t func)
  1786. {
  1787. __be32 err;
  1788. struct file *file;
  1789. loff_t offset = *offsetp;
  1790. int may_flags = NFSD_MAY_READ;
  1791. /* NFSv2 only supports 32 bit cookies */
  1792. if (rqstp->rq_vers > 2)
  1793. may_flags |= NFSD_MAY_64BIT_COOKIE;
  1794. err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
  1795. if (err)
  1796. goto out;
  1797. offset = vfs_llseek(file, offset, SEEK_SET);
  1798. if (offset < 0) {
  1799. err = nfserrno((int)offset);
  1800. goto out_close;
  1801. }
  1802. err = nfsd_buffered_readdir(file, func, cdp, offsetp);
  1803. if (err == nfserr_eof || err == nfserr_toosmall)
  1804. err = nfs_ok; /* can still be found in ->err */
  1805. out_close:
  1806. fput(file);
  1807. out:
  1808. return err;
  1809. }
  1810. /*
  1811. * Get file system stats
  1812. * N.B. After this call fhp needs an fh_put
  1813. */
  1814. __be32
  1815. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1816. {
  1817. __be32 err;
  1818. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1819. if (!err) {
  1820. struct path path = {
  1821. .mnt = fhp->fh_export->ex_path.mnt,
  1822. .dentry = fhp->fh_dentry,
  1823. };
  1824. if (vfs_statfs(&path, stat))
  1825. err = nfserr_io;
  1826. }
  1827. return err;
  1828. }
  1829. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1830. {
  1831. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1832. }
  1833. #ifdef CONFIG_NFSD_V4
  1834. /*
  1835. * Helper function to translate error numbers. In the case of xattr operations,
  1836. * some error codes need to be translated outside of the standard translations.
  1837. *
  1838. * ENODATA needs to be translated to nfserr_noxattr.
  1839. * E2BIG to nfserr_xattr2big.
  1840. *
  1841. * Additionally, vfs_listxattr can return -ERANGE. This means that the
  1842. * file has too many extended attributes to retrieve inside an
  1843. * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation:
  1844. * filesystems will allow the adding of extended attributes until they hit
  1845. * their own internal limit. This limit may be larger than XATTR_LIST_MAX.
  1846. * So, at that point, the attributes are present and valid, but can't
  1847. * be retrieved using listxattr, since the upper level xattr code enforces
  1848. * the XATTR_LIST_MAX limit.
  1849. *
  1850. * This bug means that we need to deal with listxattr returning -ERANGE. The
  1851. * best mapping is to return TOOSMALL.
  1852. */
  1853. static __be32
  1854. nfsd_xattr_errno(int err)
  1855. {
  1856. switch (err) {
  1857. case -ENODATA:
  1858. return nfserr_noxattr;
  1859. case -E2BIG:
  1860. return nfserr_xattr2big;
  1861. case -ERANGE:
  1862. return nfserr_toosmall;
  1863. }
  1864. return nfserrno(err);
  1865. }
  1866. /*
  1867. * Retrieve the specified user extended attribute. To avoid always
  1868. * having to allocate the maximum size (since we are not getting
  1869. * a maximum size from the RPC), do a probe + alloc. Hold a reader
  1870. * lock on i_rwsem to prevent the extended attribute from changing
  1871. * size while we're doing this.
  1872. */
  1873. __be32
  1874. nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
  1875. void **bufp, int *lenp)
  1876. {
  1877. ssize_t len;
  1878. __be32 err;
  1879. char *buf;
  1880. struct inode *inode;
  1881. struct dentry *dentry;
  1882. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
  1883. if (err)
  1884. return err;
  1885. err = nfs_ok;
  1886. dentry = fhp->fh_dentry;
  1887. inode = d_inode(dentry);
  1888. inode_lock_shared(inode);
  1889. len = vfs_getxattr(dentry, name, NULL, 0);
  1890. /*
  1891. * Zero-length attribute, just return.
  1892. */
  1893. if (len == 0) {
  1894. *bufp = NULL;
  1895. *lenp = 0;
  1896. goto out;
  1897. }
  1898. if (len < 0) {
  1899. err = nfsd_xattr_errno(len);
  1900. goto out;
  1901. }
  1902. if (len > *lenp) {
  1903. err = nfserr_toosmall;
  1904. goto out;
  1905. }
  1906. buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
  1907. if (buf == NULL) {
  1908. err = nfserr_jukebox;
  1909. goto out;
  1910. }
  1911. len = vfs_getxattr(dentry, name, buf, len);
  1912. if (len <= 0) {
  1913. kvfree(buf);
  1914. buf = NULL;
  1915. err = nfsd_xattr_errno(len);
  1916. }
  1917. *lenp = len;
  1918. *bufp = buf;
  1919. out:
  1920. inode_unlock_shared(inode);
  1921. return err;
  1922. }
  1923. /*
  1924. * Retrieve the xattr names. Since we can't know how many are
  1925. * user extended attributes, we must get all attributes here,
  1926. * and have the XDR encode filter out the "user." ones.
  1927. *
  1928. * While this could always just allocate an XATTR_LIST_MAX
  1929. * buffer, that's a waste, so do a probe + allocate. To
  1930. * avoid any changes between the probe and allocate, wrap
  1931. * this in inode_lock.
  1932. */
  1933. __be32
  1934. nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
  1935. int *lenp)
  1936. {
  1937. ssize_t len;
  1938. __be32 err;
  1939. char *buf;
  1940. struct inode *inode;
  1941. struct dentry *dentry;
  1942. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
  1943. if (err)
  1944. return err;
  1945. dentry = fhp->fh_dentry;
  1946. inode = d_inode(dentry);
  1947. *lenp = 0;
  1948. inode_lock_shared(inode);
  1949. len = vfs_listxattr(dentry, NULL, 0);
  1950. if (len <= 0) {
  1951. err = nfsd_xattr_errno(len);
  1952. goto out;
  1953. }
  1954. if (len > XATTR_LIST_MAX) {
  1955. err = nfserr_xattr2big;
  1956. goto out;
  1957. }
  1958. /*
  1959. * We're holding i_rwsem - use GFP_NOFS.
  1960. */
  1961. buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
  1962. if (buf == NULL) {
  1963. err = nfserr_jukebox;
  1964. goto out;
  1965. }
  1966. len = vfs_listxattr(dentry, buf, len);
  1967. if (len <= 0) {
  1968. kvfree(buf);
  1969. err = nfsd_xattr_errno(len);
  1970. goto out;
  1971. }
  1972. *lenp = len;
  1973. *bufp = buf;
  1974. err = nfs_ok;
  1975. out:
  1976. inode_unlock_shared(inode);
  1977. return err;
  1978. }
  1979. /*
  1980. * Removexattr and setxattr need to call fh_lock to both lock the inode
  1981. * and set the change attribute. Since the top-level vfs_removexattr
  1982. * and vfs_setxattr calls already do their own inode_lock calls, call
  1983. * the _locked variant. Pass in a NULL pointer for delegated_inode,
  1984. * and let the client deal with NFS4ERR_DELAY (same as with e.g.
  1985. * setattr and remove).
  1986. */
  1987. __be32
  1988. nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
  1989. {
  1990. __be32 err;
  1991. int ret;
  1992. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
  1993. if (err)
  1994. return err;
  1995. ret = fh_want_write(fhp);
  1996. if (ret)
  1997. return nfserrno(ret);
  1998. fh_lock(fhp);
  1999. ret = __vfs_removexattr_locked(fhp->fh_dentry, name, NULL);
  2000. fh_unlock(fhp);
  2001. fh_drop_write(fhp);
  2002. return nfsd_xattr_errno(ret);
  2003. }
  2004. __be32
  2005. nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
  2006. void *buf, u32 len, u32 flags)
  2007. {
  2008. __be32 err;
  2009. int ret;
  2010. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
  2011. if (err)
  2012. return err;
  2013. ret = fh_want_write(fhp);
  2014. if (ret)
  2015. return nfserrno(ret);
  2016. fh_lock(fhp);
  2017. ret = __vfs_setxattr_locked(fhp->fh_dentry, name, buf, len, flags,
  2018. NULL);
  2019. fh_unlock(fhp);
  2020. fh_drop_write(fhp);
  2021. return nfsd_xattr_errno(ret);
  2022. }
  2023. #endif
  2024. /*
  2025. * Check for a user's access permissions to this inode.
  2026. */
  2027. __be32
  2028. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  2029. struct dentry *dentry, int acc)
  2030. {
  2031. struct inode *inode = d_inode(dentry);
  2032. int err;
  2033. if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
  2034. return 0;
  2035. #if 0
  2036. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  2037. acc,
  2038. (acc & NFSD_MAY_READ)? " read" : "",
  2039. (acc & NFSD_MAY_WRITE)? " write" : "",
  2040. (acc & NFSD_MAY_EXEC)? " exec" : "",
  2041. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  2042. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  2043. (acc & NFSD_MAY_LOCK)? " lock" : "",
  2044. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  2045. inode->i_mode,
  2046. IS_IMMUTABLE(inode)? " immut" : "",
  2047. IS_APPEND(inode)? " append" : "",
  2048. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  2049. dprintk(" owner %d/%d user %d/%d\n",
  2050. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  2051. #endif
  2052. /* Normally we reject any write/sattr etc access on a read-only file
  2053. * system. But if it is IRIX doing check on write-access for a
  2054. * device special file, we ignore rofs.
  2055. */
  2056. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  2057. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  2058. if (exp_rdonly(rqstp, exp) ||
  2059. __mnt_is_readonly(exp->ex_path.mnt))
  2060. return nfserr_rofs;
  2061. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  2062. return nfserr_perm;
  2063. }
  2064. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  2065. return nfserr_perm;
  2066. if (acc & NFSD_MAY_LOCK) {
  2067. /* If we cannot rely on authentication in NLM requests,
  2068. * just allow locks, otherwise require read permission, or
  2069. * ownership
  2070. */
  2071. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  2072. return 0;
  2073. else
  2074. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  2075. }
  2076. /*
  2077. * The file owner always gets access permission for accesses that
  2078. * would normally be checked at open time. This is to make
  2079. * file access work even when the client has done a fchmod(fd, 0).
  2080. *
  2081. * However, `cp foo bar' should fail nevertheless when bar is
  2082. * readonly. A sensible way to do this might be to reject all
  2083. * attempts to truncate a read-only file, because a creat() call
  2084. * always implies file truncation.
  2085. * ... but this isn't really fair. A process may reasonably call
  2086. * ftruncate on an open file descriptor on a file with perm 000.
  2087. * We must trust the client to do permission checking - using "ACCESS"
  2088. * with NFSv3.
  2089. */
  2090. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  2091. uid_eq(inode->i_uid, current_fsuid()))
  2092. return 0;
  2093. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  2094. err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
  2095. /* Allow read access to binaries even when mode 111 */
  2096. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  2097. (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
  2098. acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
  2099. err = inode_permission(inode, MAY_EXEC);
  2100. return err? nfserrno(err) : 0;
  2101. }