vfs.c 53 KB

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