vfs.c 53 KB

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