vfs.c 48 KB

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