vfs.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  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. __be32 nfsd_finish_read(struct file *file, unsigned long *count, int host_err)
  728. {
  729. if (host_err >= 0) {
  730. nfsdstats.io_read += host_err;
  731. *count = host_err;
  732. fsnotify_access(file);
  733. return 0;
  734. } else
  735. return nfserrno(host_err);
  736. }
  737. int nfsd_splice_read(struct svc_rqst *rqstp,
  738. struct file *file, loff_t offset, unsigned long *count)
  739. {
  740. struct splice_desc sd = {
  741. .len = 0,
  742. .total_len = *count,
  743. .pos = offset,
  744. .u.data = rqstp,
  745. };
  746. int host_err;
  747. rqstp->rq_next_page = rqstp->rq_respages + 1;
  748. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  749. return nfsd_finish_read(file, count, host_err);
  750. }
  751. int nfsd_readv(struct file *file, loff_t offset, struct kvec *vec, int vlen,
  752. unsigned long *count)
  753. {
  754. mm_segment_t oldfs;
  755. int host_err;
  756. oldfs = get_fs();
  757. set_fs(KERNEL_DS);
  758. host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
  759. set_fs(oldfs);
  760. return nfsd_finish_read(file, count, host_err);
  761. }
  762. static __be32
  763. nfsd_vfs_read(struct svc_rqst *rqstp, struct file *file,
  764. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  765. {
  766. if (file->f_op->splice_read && rqstp->rq_splice_ok)
  767. return nfsd_splice_read(rqstp, file, offset, count);
  768. else
  769. return nfsd_readv(file, offset, vec, vlen, count);
  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. unsigned int pflags = current->flags;
  820. if (rqstp->rq_local)
  821. /*
  822. * We want less throttling in balance_dirty_pages()
  823. * and shrink_inactive_list() so that nfs to
  824. * localhost doesn't cause nfsd to lock up due to all
  825. * the client's dirty pages or its congested queue.
  826. */
  827. current->flags |= PF_LESS_THROTTLE;
  828. dentry = file->f_path.dentry;
  829. inode = dentry->d_inode;
  830. exp = fhp->fh_export;
  831. use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
  832. if (!EX_ISSYNC(exp))
  833. stable = 0;
  834. /* Write the data. */
  835. oldfs = get_fs(); set_fs(KERNEL_DS);
  836. host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
  837. set_fs(oldfs);
  838. if (host_err < 0)
  839. goto out_nfserr;
  840. *cnt = host_err;
  841. nfsdstats.io_write += host_err;
  842. fsnotify_modify(file);
  843. if (stable) {
  844. if (use_wgather)
  845. host_err = wait_for_concurrent_writes(file);
  846. else
  847. host_err = vfs_fsync_range(file, offset, offset+*cnt, 0);
  848. }
  849. out_nfserr:
  850. dprintk("nfsd: write complete host_err=%d\n", host_err);
  851. if (host_err >= 0)
  852. err = 0;
  853. else
  854. err = nfserrno(host_err);
  855. if (rqstp->rq_local)
  856. tsk_restore_flags(current, pflags, PF_LESS_THROTTLE);
  857. return err;
  858. }
  859. __be32 nfsd_get_tmp_read_open(struct svc_rqst *rqstp, struct svc_fh *fhp,
  860. struct file **file, struct raparms **ra)
  861. {
  862. struct inode *inode;
  863. __be32 err;
  864. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, file);
  865. if (err)
  866. return err;
  867. inode = file_inode(*file);
  868. /* Get readahead parameters */
  869. *ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
  870. if (*ra && (*ra)->p_set)
  871. (*file)->f_ra = (*ra)->p_ra;
  872. return nfs_ok;
  873. }
  874. void nfsd_put_tmp_read_open(struct file *file, struct raparms *ra)
  875. {
  876. /* Write back readahead params */
  877. if (ra) {
  878. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  879. spin_lock(&rab->pb_lock);
  880. ra->p_ra = file->f_ra;
  881. ra->p_set = 1;
  882. ra->p_count--;
  883. spin_unlock(&rab->pb_lock);
  884. }
  885. nfsd_close(file);
  886. }
  887. /*
  888. * Read data from a file. count must contain the requested read count
  889. * on entry. On return, *count contains the number of bytes actually read.
  890. * N.B. After this call fhp needs an fh_put
  891. */
  892. __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  893. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  894. {
  895. struct file *file;
  896. struct raparms *ra;
  897. __be32 err;
  898. err = nfsd_get_tmp_read_open(rqstp, fhp, &file, &ra);
  899. if (err)
  900. return err;
  901. err = nfsd_vfs_read(rqstp, file, offset, vec, vlen, count);
  902. nfsd_put_tmp_read_open(file, ra);
  903. return err;
  904. }
  905. /*
  906. * Write data to a file.
  907. * The stable flag requests synchronous writes.
  908. * N.B. After this call fhp needs an fh_put
  909. */
  910. __be32
  911. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  912. loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
  913. int *stablep)
  914. {
  915. __be32 err = 0;
  916. if (file) {
  917. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  918. NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
  919. if (err)
  920. goto out;
  921. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
  922. stablep);
  923. } else {
  924. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  925. if (err)
  926. goto out;
  927. if (cnt)
  928. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
  929. cnt, stablep);
  930. nfsd_close(file);
  931. }
  932. out:
  933. return err;
  934. }
  935. #ifdef CONFIG_NFSD_V3
  936. /*
  937. * Commit all pending writes to stable storage.
  938. *
  939. * Note: we only guarantee that data that lies within the range specified
  940. * by the 'offset' and 'count' parameters will be synced.
  941. *
  942. * Unfortunately we cannot lock the file to make sure we return full WCC
  943. * data to the client, as locking happens lower down in the filesystem.
  944. */
  945. __be32
  946. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  947. loff_t offset, unsigned long count)
  948. {
  949. struct file *file;
  950. loff_t end = LLONG_MAX;
  951. __be32 err = nfserr_inval;
  952. if (offset < 0)
  953. goto out;
  954. if (count != 0) {
  955. end = offset + (loff_t)count - 1;
  956. if (end < offset)
  957. goto out;
  958. }
  959. err = nfsd_open(rqstp, fhp, S_IFREG,
  960. NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
  961. if (err)
  962. goto out;
  963. if (EX_ISSYNC(fhp->fh_export)) {
  964. int err2 = vfs_fsync_range(file, offset, end, 0);
  965. if (err2 != -EINVAL)
  966. err = nfserrno(err2);
  967. else
  968. err = nfserr_notsupp;
  969. }
  970. nfsd_close(file);
  971. out:
  972. return err;
  973. }
  974. #endif /* CONFIG_NFSD_V3 */
  975. static __be32
  976. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
  977. struct iattr *iap)
  978. {
  979. /*
  980. * Mode has already been set earlier in create:
  981. */
  982. iap->ia_valid &= ~ATTR_MODE;
  983. /*
  984. * Setting uid/gid works only for root. Irix appears to
  985. * send along the gid on create when it tries to implement
  986. * setgid directories via NFS:
  987. */
  988. if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  989. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  990. if (iap->ia_valid)
  991. return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  992. return 0;
  993. }
  994. /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
  995. * setting size to 0 may fail for some specific file systems by the permission
  996. * checking which requires WRITE permission but the mode is 000.
  997. * we ignore the resizing(to 0) on the just new created file, since the size is
  998. * 0 after file created.
  999. *
  1000. * call this only after vfs_create() is called.
  1001. * */
  1002. static void
  1003. nfsd_check_ignore_resizing(struct iattr *iap)
  1004. {
  1005. if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
  1006. iap->ia_valid &= ~ATTR_SIZE;
  1007. }
  1008. /*
  1009. * Create a file (regular, directory, device, fifo); UNIX sockets
  1010. * not yet implemented.
  1011. * If the response fh has been verified, the parent directory should
  1012. * already be locked. Note that the parent directory is left locked.
  1013. *
  1014. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1015. */
  1016. __be32
  1017. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1018. char *fname, int flen, struct iattr *iap,
  1019. int type, dev_t rdev, struct svc_fh *resfhp)
  1020. {
  1021. struct dentry *dentry, *dchild = NULL;
  1022. struct inode *dirp;
  1023. __be32 err;
  1024. __be32 err2;
  1025. int host_err;
  1026. err = nfserr_perm;
  1027. if (!flen)
  1028. goto out;
  1029. err = nfserr_exist;
  1030. if (isdotent(fname, flen))
  1031. goto out;
  1032. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1033. if (err)
  1034. goto out;
  1035. dentry = fhp->fh_dentry;
  1036. dirp = dentry->d_inode;
  1037. err = nfserr_notdir;
  1038. if (!dirp->i_op->lookup)
  1039. goto out;
  1040. /*
  1041. * Check whether the response file handle has been verified yet.
  1042. * If it has, the parent directory should already be locked.
  1043. */
  1044. if (!resfhp->fh_dentry) {
  1045. host_err = fh_want_write(fhp);
  1046. if (host_err)
  1047. goto out_nfserr;
  1048. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1049. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1050. dchild = lookup_one_len(fname, dentry, flen);
  1051. host_err = PTR_ERR(dchild);
  1052. if (IS_ERR(dchild))
  1053. goto out_nfserr;
  1054. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1055. if (err)
  1056. goto out;
  1057. } else {
  1058. /* called from nfsd_proc_create */
  1059. dchild = dget(resfhp->fh_dentry);
  1060. if (!fhp->fh_locked) {
  1061. /* not actually possible */
  1062. printk(KERN_ERR
  1063. "nfsd_create: parent %pd2 not locked!\n",
  1064. dentry);
  1065. err = nfserr_io;
  1066. goto out;
  1067. }
  1068. }
  1069. /*
  1070. * Make sure the child dentry is still negative ...
  1071. */
  1072. err = nfserr_exist;
  1073. if (dchild->d_inode) {
  1074. dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
  1075. dentry, dchild);
  1076. goto out;
  1077. }
  1078. if (!(iap->ia_valid & ATTR_MODE))
  1079. iap->ia_mode = 0;
  1080. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1081. err = nfserr_inval;
  1082. if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
  1083. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1084. type);
  1085. goto out;
  1086. }
  1087. /*
  1088. * Get the dir op function pointer.
  1089. */
  1090. err = 0;
  1091. host_err = 0;
  1092. switch (type) {
  1093. case S_IFREG:
  1094. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1095. if (!host_err)
  1096. nfsd_check_ignore_resizing(iap);
  1097. break;
  1098. case S_IFDIR:
  1099. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1100. break;
  1101. case S_IFCHR:
  1102. case S_IFBLK:
  1103. case S_IFIFO:
  1104. case S_IFSOCK:
  1105. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1106. break;
  1107. }
  1108. if (host_err < 0)
  1109. goto out_nfserr;
  1110. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1111. /*
  1112. * nfsd_setattr already committed the child. Transactional filesystems
  1113. * had a chance to commit changes for both parent and child
  1114. * simultaneously making the following commit_metadata a noop.
  1115. */
  1116. err2 = nfserrno(commit_metadata(fhp));
  1117. if (err2)
  1118. err = err2;
  1119. /*
  1120. * Update the file handle to get the new inode info.
  1121. */
  1122. if (!err)
  1123. err = fh_update(resfhp);
  1124. out:
  1125. if (dchild && !IS_ERR(dchild))
  1126. dput(dchild);
  1127. return err;
  1128. out_nfserr:
  1129. err = nfserrno(host_err);
  1130. goto out;
  1131. }
  1132. #ifdef CONFIG_NFSD_V3
  1133. static inline int nfsd_create_is_exclusive(int createmode)
  1134. {
  1135. return createmode == NFS3_CREATE_EXCLUSIVE
  1136. || createmode == NFS4_CREATE_EXCLUSIVE4_1;
  1137. }
  1138. /*
  1139. * NFSv3 and NFSv4 version of nfsd_create
  1140. */
  1141. __be32
  1142. do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1143. char *fname, int flen, struct iattr *iap,
  1144. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1145. bool *truncp, bool *created)
  1146. {
  1147. struct dentry *dentry, *dchild = NULL;
  1148. struct inode *dirp;
  1149. __be32 err;
  1150. int host_err;
  1151. __u32 v_mtime=0, v_atime=0;
  1152. err = nfserr_perm;
  1153. if (!flen)
  1154. goto out;
  1155. err = nfserr_exist;
  1156. if (isdotent(fname, flen))
  1157. goto out;
  1158. if (!(iap->ia_valid & ATTR_MODE))
  1159. iap->ia_mode = 0;
  1160. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  1161. if (err)
  1162. goto out;
  1163. dentry = fhp->fh_dentry;
  1164. dirp = dentry->d_inode;
  1165. /* Get all the sanity checks out of the way before
  1166. * we lock the parent. */
  1167. err = nfserr_notdir;
  1168. if (!dirp->i_op->lookup)
  1169. goto out;
  1170. host_err = fh_want_write(fhp);
  1171. if (host_err)
  1172. goto out_nfserr;
  1173. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1174. /*
  1175. * Compose the response file handle.
  1176. */
  1177. dchild = lookup_one_len(fname, dentry, flen);
  1178. host_err = PTR_ERR(dchild);
  1179. if (IS_ERR(dchild))
  1180. goto out_nfserr;
  1181. /* If file doesn't exist, check for permissions to create one */
  1182. if (!dchild->d_inode) {
  1183. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1184. if (err)
  1185. goto out;
  1186. }
  1187. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1188. if (err)
  1189. goto out;
  1190. if (nfsd_create_is_exclusive(createmode)) {
  1191. /* solaris7 gets confused (bugid 4218508) if these have
  1192. * the high bit set, so just clear the high bits. If this is
  1193. * ever changed to use different attrs for storing the
  1194. * verifier, then do_open_lookup() will also need to be fixed
  1195. * accordingly.
  1196. */
  1197. v_mtime = verifier[0]&0x7fffffff;
  1198. v_atime = verifier[1]&0x7fffffff;
  1199. }
  1200. if (dchild->d_inode) {
  1201. err = 0;
  1202. switch (createmode) {
  1203. case NFS3_CREATE_UNCHECKED:
  1204. if (! S_ISREG(dchild->d_inode->i_mode))
  1205. goto out;
  1206. else if (truncp) {
  1207. /* in nfsv4, we need to treat this case a little
  1208. * differently. we don't want to truncate the
  1209. * file now; this would be wrong if the OPEN
  1210. * fails for some other reason. furthermore,
  1211. * if the size is nonzero, we should ignore it
  1212. * according to spec!
  1213. */
  1214. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1215. }
  1216. else {
  1217. iap->ia_valid &= ATTR_SIZE;
  1218. goto set_attr;
  1219. }
  1220. break;
  1221. case NFS3_CREATE_EXCLUSIVE:
  1222. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1223. && dchild->d_inode->i_atime.tv_sec == v_atime
  1224. && dchild->d_inode->i_size == 0 ) {
  1225. if (created)
  1226. *created = 1;
  1227. break;
  1228. }
  1229. case NFS4_CREATE_EXCLUSIVE4_1:
  1230. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1231. && dchild->d_inode->i_atime.tv_sec == v_atime
  1232. && dchild->d_inode->i_size == 0 ) {
  1233. if (created)
  1234. *created = 1;
  1235. goto set_attr;
  1236. }
  1237. /* fallthru */
  1238. case NFS3_CREATE_GUARDED:
  1239. err = nfserr_exist;
  1240. }
  1241. fh_drop_write(fhp);
  1242. goto out;
  1243. }
  1244. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1245. if (host_err < 0) {
  1246. fh_drop_write(fhp);
  1247. goto out_nfserr;
  1248. }
  1249. if (created)
  1250. *created = 1;
  1251. nfsd_check_ignore_resizing(iap);
  1252. if (nfsd_create_is_exclusive(createmode)) {
  1253. /* Cram the verifier into atime/mtime */
  1254. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1255. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1256. /* XXX someone who knows this better please fix it for nsec */
  1257. iap->ia_mtime.tv_sec = v_mtime;
  1258. iap->ia_atime.tv_sec = v_atime;
  1259. iap->ia_mtime.tv_nsec = 0;
  1260. iap->ia_atime.tv_nsec = 0;
  1261. }
  1262. set_attr:
  1263. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1264. /*
  1265. * nfsd_setattr already committed the child (and possibly also the parent).
  1266. */
  1267. if (!err)
  1268. err = nfserrno(commit_metadata(fhp));
  1269. /*
  1270. * Update the filehandle to get the new inode info.
  1271. */
  1272. if (!err)
  1273. err = fh_update(resfhp);
  1274. out:
  1275. fh_unlock(fhp);
  1276. if (dchild && !IS_ERR(dchild))
  1277. dput(dchild);
  1278. fh_drop_write(fhp);
  1279. return err;
  1280. out_nfserr:
  1281. err = nfserrno(host_err);
  1282. goto out;
  1283. }
  1284. #endif /* CONFIG_NFSD_V3 */
  1285. /*
  1286. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1287. * fits into the buffer. On return, it contains the true length.
  1288. * N.B. After this call fhp needs an fh_put
  1289. */
  1290. __be32
  1291. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1292. {
  1293. struct inode *inode;
  1294. mm_segment_t oldfs;
  1295. __be32 err;
  1296. int host_err;
  1297. struct path path;
  1298. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1299. if (err)
  1300. goto out;
  1301. path.mnt = fhp->fh_export->ex_path.mnt;
  1302. path.dentry = fhp->fh_dentry;
  1303. inode = path.dentry->d_inode;
  1304. err = nfserr_inval;
  1305. if (!inode->i_op->readlink)
  1306. goto out;
  1307. touch_atime(&path);
  1308. /* N.B. Why does this call need a get_fs()??
  1309. * Remove the set_fs and watch the fireworks:-) --okir
  1310. */
  1311. oldfs = get_fs(); set_fs(KERNEL_DS);
  1312. host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
  1313. set_fs(oldfs);
  1314. if (host_err < 0)
  1315. goto out_nfserr;
  1316. *lenp = host_err;
  1317. err = 0;
  1318. out:
  1319. return err;
  1320. out_nfserr:
  1321. err = nfserrno(host_err);
  1322. goto out;
  1323. }
  1324. /*
  1325. * Create a symlink and look up its inode
  1326. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1327. */
  1328. __be32
  1329. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1330. char *fname, int flen,
  1331. char *path, int plen,
  1332. struct svc_fh *resfhp,
  1333. struct iattr *iap)
  1334. {
  1335. struct dentry *dentry, *dnew;
  1336. __be32 err, cerr;
  1337. int host_err;
  1338. err = nfserr_noent;
  1339. if (!flen || !plen)
  1340. goto out;
  1341. err = nfserr_exist;
  1342. if (isdotent(fname, flen))
  1343. goto out;
  1344. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1345. if (err)
  1346. goto out;
  1347. host_err = fh_want_write(fhp);
  1348. if (host_err)
  1349. goto out_nfserr;
  1350. fh_lock(fhp);
  1351. dentry = fhp->fh_dentry;
  1352. dnew = lookup_one_len(fname, dentry, flen);
  1353. host_err = PTR_ERR(dnew);
  1354. if (IS_ERR(dnew))
  1355. goto out_nfserr;
  1356. if (unlikely(path[plen] != 0)) {
  1357. char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
  1358. if (path_alloced == NULL)
  1359. host_err = -ENOMEM;
  1360. else {
  1361. strncpy(path_alloced, path, plen);
  1362. path_alloced[plen] = 0;
  1363. host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
  1364. kfree(path_alloced);
  1365. }
  1366. } else
  1367. host_err = vfs_symlink(dentry->d_inode, dnew, path);
  1368. err = nfserrno(host_err);
  1369. if (!err)
  1370. err = nfserrno(commit_metadata(fhp));
  1371. fh_unlock(fhp);
  1372. fh_drop_write(fhp);
  1373. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1374. dput(dnew);
  1375. if (err==0) err = cerr;
  1376. out:
  1377. return err;
  1378. out_nfserr:
  1379. err = nfserrno(host_err);
  1380. goto out;
  1381. }
  1382. /*
  1383. * Create a hardlink
  1384. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1385. */
  1386. __be32
  1387. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1388. char *name, int len, struct svc_fh *tfhp)
  1389. {
  1390. struct dentry *ddir, *dnew, *dold;
  1391. struct inode *dirp;
  1392. __be32 err;
  1393. int host_err;
  1394. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1395. if (err)
  1396. goto out;
  1397. err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
  1398. if (err)
  1399. goto out;
  1400. err = nfserr_isdir;
  1401. if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode))
  1402. goto out;
  1403. err = nfserr_perm;
  1404. if (!len)
  1405. goto out;
  1406. err = nfserr_exist;
  1407. if (isdotent(name, len))
  1408. goto out;
  1409. host_err = fh_want_write(tfhp);
  1410. if (host_err) {
  1411. err = nfserrno(host_err);
  1412. goto out;
  1413. }
  1414. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1415. ddir = ffhp->fh_dentry;
  1416. dirp = ddir->d_inode;
  1417. dnew = lookup_one_len(name, ddir, len);
  1418. host_err = PTR_ERR(dnew);
  1419. if (IS_ERR(dnew))
  1420. goto out_nfserr;
  1421. dold = tfhp->fh_dentry;
  1422. err = nfserr_noent;
  1423. if (!dold->d_inode)
  1424. goto out_dput;
  1425. host_err = vfs_link(dold, dirp, dnew, NULL);
  1426. if (!host_err) {
  1427. err = nfserrno(commit_metadata(ffhp));
  1428. if (!err)
  1429. err = nfserrno(commit_metadata(tfhp));
  1430. } else {
  1431. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1432. err = nfserr_acces;
  1433. else
  1434. err = nfserrno(host_err);
  1435. }
  1436. out_dput:
  1437. dput(dnew);
  1438. out_unlock:
  1439. fh_unlock(ffhp);
  1440. fh_drop_write(tfhp);
  1441. out:
  1442. return err;
  1443. out_nfserr:
  1444. err = nfserrno(host_err);
  1445. goto out_unlock;
  1446. }
  1447. /*
  1448. * Rename a file
  1449. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1450. */
  1451. __be32
  1452. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1453. struct svc_fh *tfhp, char *tname, int tlen)
  1454. {
  1455. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1456. struct inode *fdir, *tdir;
  1457. __be32 err;
  1458. int host_err;
  1459. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1460. if (err)
  1461. goto out;
  1462. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1463. if (err)
  1464. goto out;
  1465. fdentry = ffhp->fh_dentry;
  1466. fdir = fdentry->d_inode;
  1467. tdentry = tfhp->fh_dentry;
  1468. tdir = tdentry->d_inode;
  1469. err = nfserr_perm;
  1470. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1471. goto out;
  1472. host_err = fh_want_write(ffhp);
  1473. if (host_err) {
  1474. err = nfserrno(host_err);
  1475. goto out;
  1476. }
  1477. /* cannot use fh_lock as we need deadlock protective ordering
  1478. * so do it by hand */
  1479. trap = lock_rename(tdentry, fdentry);
  1480. ffhp->fh_locked = tfhp->fh_locked = 1;
  1481. fill_pre_wcc(ffhp);
  1482. fill_pre_wcc(tfhp);
  1483. odentry = lookup_one_len(fname, fdentry, flen);
  1484. host_err = PTR_ERR(odentry);
  1485. if (IS_ERR(odentry))
  1486. goto out_nfserr;
  1487. host_err = -ENOENT;
  1488. if (!odentry->d_inode)
  1489. goto out_dput_old;
  1490. host_err = -EINVAL;
  1491. if (odentry == trap)
  1492. goto out_dput_old;
  1493. ndentry = lookup_one_len(tname, tdentry, tlen);
  1494. host_err = PTR_ERR(ndentry);
  1495. if (IS_ERR(ndentry))
  1496. goto out_dput_old;
  1497. host_err = -ENOTEMPTY;
  1498. if (ndentry == trap)
  1499. goto out_dput_new;
  1500. host_err = -EXDEV;
  1501. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1502. goto out_dput_new;
  1503. if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
  1504. goto out_dput_new;
  1505. host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
  1506. if (!host_err) {
  1507. host_err = commit_metadata(tfhp);
  1508. if (!host_err)
  1509. host_err = commit_metadata(ffhp);
  1510. }
  1511. out_dput_new:
  1512. dput(ndentry);
  1513. out_dput_old:
  1514. dput(odentry);
  1515. out_nfserr:
  1516. err = nfserrno(host_err);
  1517. /*
  1518. * We cannot rely on fh_unlock on the two filehandles,
  1519. * as that would do the wrong thing if the two directories
  1520. * were the same, so again we do it by hand.
  1521. */
  1522. fill_post_wcc(ffhp);
  1523. fill_post_wcc(tfhp);
  1524. unlock_rename(tdentry, fdentry);
  1525. ffhp->fh_locked = tfhp->fh_locked = 0;
  1526. fh_drop_write(ffhp);
  1527. out:
  1528. return err;
  1529. }
  1530. /*
  1531. * Unlink a file or directory
  1532. * N.B. After this call fhp needs an fh_put
  1533. */
  1534. __be32
  1535. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1536. char *fname, int flen)
  1537. {
  1538. struct dentry *dentry, *rdentry;
  1539. struct inode *dirp;
  1540. __be32 err;
  1541. int host_err;
  1542. err = nfserr_acces;
  1543. if (!flen || isdotent(fname, flen))
  1544. goto out;
  1545. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1546. if (err)
  1547. goto out;
  1548. host_err = fh_want_write(fhp);
  1549. if (host_err)
  1550. goto out_nfserr;
  1551. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1552. dentry = fhp->fh_dentry;
  1553. dirp = dentry->d_inode;
  1554. rdentry = lookup_one_len(fname, dentry, flen);
  1555. host_err = PTR_ERR(rdentry);
  1556. if (IS_ERR(rdentry))
  1557. goto out_nfserr;
  1558. if (!rdentry->d_inode) {
  1559. dput(rdentry);
  1560. err = nfserr_noent;
  1561. goto out;
  1562. }
  1563. if (!type)
  1564. type = rdentry->d_inode->i_mode & S_IFMT;
  1565. if (type != S_IFDIR)
  1566. host_err = vfs_unlink(dirp, rdentry, NULL);
  1567. else
  1568. host_err = vfs_rmdir(dirp, rdentry);
  1569. if (!host_err)
  1570. host_err = commit_metadata(fhp);
  1571. dput(rdentry);
  1572. out_nfserr:
  1573. err = nfserrno(host_err);
  1574. out:
  1575. return err;
  1576. }
  1577. /*
  1578. * We do this buffering because we must not call back into the file
  1579. * system's ->lookup() method from the filldir callback. That may well
  1580. * deadlock a number of file systems.
  1581. *
  1582. * This is based heavily on the implementation of same in XFS.
  1583. */
  1584. struct buffered_dirent {
  1585. u64 ino;
  1586. loff_t offset;
  1587. int namlen;
  1588. unsigned int d_type;
  1589. char name[];
  1590. };
  1591. struct readdir_data {
  1592. struct dir_context ctx;
  1593. char *dirent;
  1594. size_t used;
  1595. int full;
  1596. };
  1597. static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
  1598. loff_t offset, u64 ino, unsigned int d_type)
  1599. {
  1600. struct readdir_data *buf = __buf;
  1601. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1602. unsigned int reclen;
  1603. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1604. if (buf->used + reclen > PAGE_SIZE) {
  1605. buf->full = 1;
  1606. return -EINVAL;
  1607. }
  1608. de->namlen = namlen;
  1609. de->offset = offset;
  1610. de->ino = ino;
  1611. de->d_type = d_type;
  1612. memcpy(de->name, name, namlen);
  1613. buf->used += reclen;
  1614. return 0;
  1615. }
  1616. static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
  1617. struct readdir_cd *cdp, loff_t *offsetp)
  1618. {
  1619. struct buffered_dirent *de;
  1620. int host_err;
  1621. int size;
  1622. loff_t offset;
  1623. struct readdir_data buf = {
  1624. .ctx.actor = nfsd_buffered_filldir,
  1625. .dirent = (void *)__get_free_page(GFP_KERNEL)
  1626. };
  1627. if (!buf.dirent)
  1628. return nfserrno(-ENOMEM);
  1629. offset = *offsetp;
  1630. while (1) {
  1631. struct inode *dir_inode = file_inode(file);
  1632. unsigned int reclen;
  1633. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1634. buf.used = 0;
  1635. buf.full = 0;
  1636. host_err = iterate_dir(file, &buf.ctx);
  1637. if (buf.full)
  1638. host_err = 0;
  1639. if (host_err < 0)
  1640. break;
  1641. size = buf.used;
  1642. if (!size)
  1643. break;
  1644. /*
  1645. * Various filldir functions may end up calling back into
  1646. * lookup_one_len() and the file system's ->lookup() method.
  1647. * These expect i_mutex to be held, as it would within readdir.
  1648. */
  1649. host_err = mutex_lock_killable(&dir_inode->i_mutex);
  1650. if (host_err)
  1651. break;
  1652. de = (struct buffered_dirent *)buf.dirent;
  1653. while (size > 0) {
  1654. offset = de->offset;
  1655. if (func(cdp, de->name, de->namlen, de->offset,
  1656. de->ino, de->d_type))
  1657. break;
  1658. if (cdp->err != nfs_ok)
  1659. break;
  1660. reclen = ALIGN(sizeof(*de) + de->namlen,
  1661. sizeof(u64));
  1662. size -= reclen;
  1663. de = (struct buffered_dirent *)((char *)de + reclen);
  1664. }
  1665. mutex_unlock(&dir_inode->i_mutex);
  1666. if (size > 0) /* We bailed out early */
  1667. break;
  1668. offset = vfs_llseek(file, 0, SEEK_CUR);
  1669. }
  1670. free_page((unsigned long)(buf.dirent));
  1671. if (host_err)
  1672. return nfserrno(host_err);
  1673. *offsetp = offset;
  1674. return cdp->err;
  1675. }
  1676. /*
  1677. * Read entries from a directory.
  1678. * The NFSv3/4 verifier we ignore for now.
  1679. */
  1680. __be32
  1681. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1682. struct readdir_cd *cdp, filldir_t func)
  1683. {
  1684. __be32 err;
  1685. struct file *file;
  1686. loff_t offset = *offsetp;
  1687. int may_flags = NFSD_MAY_READ;
  1688. /* NFSv2 only supports 32 bit cookies */
  1689. if (rqstp->rq_vers > 2)
  1690. may_flags |= NFSD_MAY_64BIT_COOKIE;
  1691. err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
  1692. if (err)
  1693. goto out;
  1694. offset = vfs_llseek(file, offset, SEEK_SET);
  1695. if (offset < 0) {
  1696. err = nfserrno((int)offset);
  1697. goto out_close;
  1698. }
  1699. err = nfsd_buffered_readdir(file, func, cdp, offsetp);
  1700. if (err == nfserr_eof || err == nfserr_toosmall)
  1701. err = nfs_ok; /* can still be found in ->err */
  1702. out_close:
  1703. nfsd_close(file);
  1704. out:
  1705. return err;
  1706. }
  1707. /*
  1708. * Get file system stats
  1709. * N.B. After this call fhp needs an fh_put
  1710. */
  1711. __be32
  1712. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1713. {
  1714. __be32 err;
  1715. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1716. if (!err) {
  1717. struct path path = {
  1718. .mnt = fhp->fh_export->ex_path.mnt,
  1719. .dentry = fhp->fh_dentry,
  1720. };
  1721. if (vfs_statfs(&path, stat))
  1722. err = nfserr_io;
  1723. }
  1724. return err;
  1725. }
  1726. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1727. {
  1728. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1729. }
  1730. /*
  1731. * Check for a user's access permissions to this inode.
  1732. */
  1733. __be32
  1734. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1735. struct dentry *dentry, int acc)
  1736. {
  1737. struct inode *inode = dentry->d_inode;
  1738. int err;
  1739. if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
  1740. return 0;
  1741. #if 0
  1742. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1743. acc,
  1744. (acc & NFSD_MAY_READ)? " read" : "",
  1745. (acc & NFSD_MAY_WRITE)? " write" : "",
  1746. (acc & NFSD_MAY_EXEC)? " exec" : "",
  1747. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  1748. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  1749. (acc & NFSD_MAY_LOCK)? " lock" : "",
  1750. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1751. inode->i_mode,
  1752. IS_IMMUTABLE(inode)? " immut" : "",
  1753. IS_APPEND(inode)? " append" : "",
  1754. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  1755. dprintk(" owner %d/%d user %d/%d\n",
  1756. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  1757. #endif
  1758. /* Normally we reject any write/sattr etc access on a read-only file
  1759. * system. But if it is IRIX doing check on write-access for a
  1760. * device special file, we ignore rofs.
  1761. */
  1762. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  1763. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  1764. if (exp_rdonly(rqstp, exp) ||
  1765. __mnt_is_readonly(exp->ex_path.mnt))
  1766. return nfserr_rofs;
  1767. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1768. return nfserr_perm;
  1769. }
  1770. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  1771. return nfserr_perm;
  1772. if (acc & NFSD_MAY_LOCK) {
  1773. /* If we cannot rely on authentication in NLM requests,
  1774. * just allow locks, otherwise require read permission, or
  1775. * ownership
  1776. */
  1777. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1778. return 0;
  1779. else
  1780. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  1781. }
  1782. /*
  1783. * The file owner always gets access permission for accesses that
  1784. * would normally be checked at open time. This is to make
  1785. * file access work even when the client has done a fchmod(fd, 0).
  1786. *
  1787. * However, `cp foo bar' should fail nevertheless when bar is
  1788. * readonly. A sensible way to do this might be to reject all
  1789. * attempts to truncate a read-only file, because a creat() call
  1790. * always implies file truncation.
  1791. * ... but this isn't really fair. A process may reasonably call
  1792. * ftruncate on an open file descriptor on a file with perm 000.
  1793. * We must trust the client to do permission checking - using "ACCESS"
  1794. * with NFSv3.
  1795. */
  1796. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  1797. uid_eq(inode->i_uid, current_fsuid()))
  1798. return 0;
  1799. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  1800. err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
  1801. /* Allow read access to binaries even when mode 111 */
  1802. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1803. (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
  1804. acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
  1805. err = inode_permission(inode, MAY_EXEC);
  1806. return err? nfserrno(err) : 0;
  1807. }
  1808. void
  1809. nfsd_racache_shutdown(void)
  1810. {
  1811. struct raparms *raparm, *last_raparm;
  1812. unsigned int i;
  1813. dprintk("nfsd: freeing readahead buffers.\n");
  1814. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1815. raparm = raparm_hash[i].pb_head;
  1816. while(raparm) {
  1817. last_raparm = raparm;
  1818. raparm = raparm->p_next;
  1819. kfree(last_raparm);
  1820. }
  1821. raparm_hash[i].pb_head = NULL;
  1822. }
  1823. }
  1824. /*
  1825. * Initialize readahead param cache
  1826. */
  1827. int
  1828. nfsd_racache_init(int cache_size)
  1829. {
  1830. int i;
  1831. int j = 0;
  1832. int nperbucket;
  1833. struct raparms **raparm = NULL;
  1834. if (raparm_hash[0].pb_head)
  1835. return 0;
  1836. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1837. if (nperbucket < 2)
  1838. nperbucket = 2;
  1839. cache_size = nperbucket * RAPARM_HASH_SIZE;
  1840. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1841. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1842. spin_lock_init(&raparm_hash[i].pb_lock);
  1843. raparm = &raparm_hash[i].pb_head;
  1844. for (j = 0; j < nperbucket; j++) {
  1845. *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
  1846. if (!*raparm)
  1847. goto out_nomem;
  1848. raparm = &(*raparm)->p_next;
  1849. }
  1850. *raparm = NULL;
  1851. }
  1852. nfsdstats.ra_size = cache_size;
  1853. return 0;
  1854. out_nomem:
  1855. dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
  1856. nfsd_racache_shutdown();
  1857. return -ENOMEM;
  1858. }