vfs.c 54 KB

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