vfs.c 50 KB

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