vfs.c 50 KB

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