dir.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. /*
  2. * linux/fs/nfs/dir.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs directory handling functions
  7. *
  8. * 10 Apr 1996 Added silly rename for unlink --okir
  9. * 28 Sep 1996 Improved directory cache --okir
  10. * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
  11. * Re-implemented silly rename for unlink, newly implemented
  12. * silly rename for nfs_rename() following the suggestions
  13. * of Olaf Kirch (okir) found in this file.
  14. * Following Linus comments on my original hack, this version
  15. * depends only on the dcache stuff and doesn't touch the inode
  16. * layer (iput() and friends).
  17. * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
  18. */
  19. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <linux/stat.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/string.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/sunrpc/clnt.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/pagevec.h>
  33. #include <linux/namei.h>
  34. #include <linux/mount.h>
  35. #include <linux/sched.h>
  36. #include "nfs4_fs.h"
  37. #include "delegation.h"
  38. #include "iostat.h"
  39. #include "internal.h"
  40. /* #define NFS_DEBUG_VERBOSE 1 */
  41. static int nfs_opendir(struct inode *, struct file *);
  42. static int nfs_readdir(struct file *, void *, filldir_t);
  43. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  44. static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
  45. static int nfs_mkdir(struct inode *, struct dentry *, int);
  46. static int nfs_rmdir(struct inode *, struct dentry *);
  47. static int nfs_unlink(struct inode *, struct dentry *);
  48. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  49. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  50. static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
  51. static int nfs_rename(struct inode *, struct dentry *,
  52. struct inode *, struct dentry *);
  53. static int nfs_fsync_dir(struct file *, struct dentry *, int);
  54. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  55. const struct file_operations nfs_dir_operations = {
  56. .llseek = nfs_llseek_dir,
  57. .read = generic_read_dir,
  58. .readdir = nfs_readdir,
  59. .open = nfs_opendir,
  60. .release = nfs_release,
  61. .fsync = nfs_fsync_dir,
  62. };
  63. const struct inode_operations nfs_dir_inode_operations = {
  64. .create = nfs_create,
  65. .lookup = nfs_lookup,
  66. .link = nfs_link,
  67. .unlink = nfs_unlink,
  68. .symlink = nfs_symlink,
  69. .mkdir = nfs_mkdir,
  70. .rmdir = nfs_rmdir,
  71. .mknod = nfs_mknod,
  72. .rename = nfs_rename,
  73. .permission = nfs_permission,
  74. .getattr = nfs_getattr,
  75. .setattr = nfs_setattr,
  76. };
  77. #ifdef CONFIG_NFS_V3
  78. const struct inode_operations nfs3_dir_inode_operations = {
  79. .create = nfs_create,
  80. .lookup = nfs_lookup,
  81. .link = nfs_link,
  82. .unlink = nfs_unlink,
  83. .symlink = nfs_symlink,
  84. .mkdir = nfs_mkdir,
  85. .rmdir = nfs_rmdir,
  86. .mknod = nfs_mknod,
  87. .rename = nfs_rename,
  88. .permission = nfs_permission,
  89. .getattr = nfs_getattr,
  90. .setattr = nfs_setattr,
  91. .listxattr = nfs3_listxattr,
  92. .getxattr = nfs3_getxattr,
  93. .setxattr = nfs3_setxattr,
  94. .removexattr = nfs3_removexattr,
  95. };
  96. #endif /* CONFIG_NFS_V3 */
  97. #ifdef CONFIG_NFS_V4
  98. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  99. const struct inode_operations nfs4_dir_inode_operations = {
  100. .create = nfs_create,
  101. .lookup = nfs_atomic_lookup,
  102. .link = nfs_link,
  103. .unlink = nfs_unlink,
  104. .symlink = nfs_symlink,
  105. .mkdir = nfs_mkdir,
  106. .rmdir = nfs_rmdir,
  107. .mknod = nfs_mknod,
  108. .rename = nfs_rename,
  109. .permission = nfs_permission,
  110. .getattr = nfs_getattr,
  111. .setattr = nfs_setattr,
  112. .getxattr = nfs4_getxattr,
  113. .setxattr = nfs4_setxattr,
  114. .listxattr = nfs4_listxattr,
  115. };
  116. #endif /* CONFIG_NFS_V4 */
  117. /*
  118. * Open file
  119. */
  120. static int
  121. nfs_opendir(struct inode *inode, struct file *filp)
  122. {
  123. int res;
  124. dfprintk(FILE, "NFS: open dir(%s/%s)\n",
  125. filp->f_path.dentry->d_parent->d_name.name,
  126. filp->f_path.dentry->d_name.name);
  127. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  128. /* Call generic open code in order to cache credentials */
  129. res = nfs_open(inode, filp);
  130. return res;
  131. }
  132. typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int);
  133. typedef struct {
  134. struct file *file;
  135. struct page *page;
  136. unsigned long page_index;
  137. __be32 *ptr;
  138. u64 *dir_cookie;
  139. loff_t current_index;
  140. struct nfs_entry *entry;
  141. decode_dirent_t decode;
  142. int plus;
  143. unsigned long timestamp;
  144. int timestamp_valid;
  145. } nfs_readdir_descriptor_t;
  146. /* Now we cache directories properly, by stuffing the dirent
  147. * data directly in the page cache.
  148. *
  149. * Inode invalidation due to refresh etc. takes care of
  150. * _everything_, no sloppy entry flushing logic, no extraneous
  151. * copying, network direct to page cache, the way it was meant
  152. * to be.
  153. *
  154. * NOTE: Dirent information verification is done always by the
  155. * page-in of the RPC reply, nowhere else, this simplies
  156. * things substantially.
  157. */
  158. static
  159. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
  160. {
  161. struct file *file = desc->file;
  162. struct inode *inode = file->f_path.dentry->d_inode;
  163. struct rpc_cred *cred = nfs_file_cred(file);
  164. unsigned long timestamp;
  165. int error;
  166. dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
  167. __func__, (long long)desc->entry->cookie,
  168. page->index);
  169. again:
  170. timestamp = jiffies;
  171. error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page,
  172. NFS_SERVER(inode)->dtsize, desc->plus);
  173. if (error < 0) {
  174. /* We requested READDIRPLUS, but the server doesn't grok it */
  175. if (error == -ENOTSUPP && desc->plus) {
  176. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  177. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  178. desc->plus = 0;
  179. goto again;
  180. }
  181. goto error;
  182. }
  183. desc->timestamp = timestamp;
  184. desc->timestamp_valid = 1;
  185. SetPageUptodate(page);
  186. /* Ensure consistent page alignment of the data.
  187. * Note: assumes we have exclusive access to this mapping either
  188. * through inode->i_mutex or some other mechanism.
  189. */
  190. if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
  191. /* Should never happen */
  192. nfs_zap_mapping(inode, inode->i_mapping);
  193. }
  194. unlock_page(page);
  195. return 0;
  196. error:
  197. unlock_page(page);
  198. return -EIO;
  199. }
  200. static inline
  201. int dir_decode(nfs_readdir_descriptor_t *desc)
  202. {
  203. __be32 *p = desc->ptr;
  204. p = desc->decode(p, desc->entry, desc->plus);
  205. if (IS_ERR(p))
  206. return PTR_ERR(p);
  207. desc->ptr = p;
  208. if (desc->timestamp_valid)
  209. desc->entry->fattr->time_start = desc->timestamp;
  210. else
  211. desc->entry->fattr->valid &= ~NFS_ATTR_FATTR;
  212. return 0;
  213. }
  214. static inline
  215. void dir_page_release(nfs_readdir_descriptor_t *desc)
  216. {
  217. kunmap(desc->page);
  218. page_cache_release(desc->page);
  219. desc->page = NULL;
  220. desc->ptr = NULL;
  221. }
  222. /*
  223. * Given a pointer to a buffer that has already been filled by a call
  224. * to readdir, find the next entry with cookie '*desc->dir_cookie'.
  225. *
  226. * If the end of the buffer has been reached, return -EAGAIN, if not,
  227. * return the offset within the buffer of the next entry to be
  228. * read.
  229. */
  230. static inline
  231. int find_dirent(nfs_readdir_descriptor_t *desc)
  232. {
  233. struct nfs_entry *entry = desc->entry;
  234. int loop_count = 0,
  235. status;
  236. while((status = dir_decode(desc)) == 0) {
  237. dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
  238. __func__, (unsigned long long)entry->cookie);
  239. if (entry->prev_cookie == *desc->dir_cookie)
  240. break;
  241. if (loop_count++ > 200) {
  242. loop_count = 0;
  243. schedule();
  244. }
  245. }
  246. return status;
  247. }
  248. /*
  249. * Given a pointer to a buffer that has already been filled by a call
  250. * to readdir, find the entry at offset 'desc->file->f_pos'.
  251. *
  252. * If the end of the buffer has been reached, return -EAGAIN, if not,
  253. * return the offset within the buffer of the next entry to be
  254. * read.
  255. */
  256. static inline
  257. int find_dirent_index(nfs_readdir_descriptor_t *desc)
  258. {
  259. struct nfs_entry *entry = desc->entry;
  260. int loop_count = 0,
  261. status;
  262. for(;;) {
  263. status = dir_decode(desc);
  264. if (status)
  265. break;
  266. dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
  267. (unsigned long long)entry->cookie, desc->current_index);
  268. if (desc->file->f_pos == desc->current_index) {
  269. *desc->dir_cookie = entry->cookie;
  270. break;
  271. }
  272. desc->current_index++;
  273. if (loop_count++ > 200) {
  274. loop_count = 0;
  275. schedule();
  276. }
  277. }
  278. return status;
  279. }
  280. /*
  281. * Find the given page, and call find_dirent() or find_dirent_index in
  282. * order to try to return the next entry.
  283. */
  284. static inline
  285. int find_dirent_page(nfs_readdir_descriptor_t *desc)
  286. {
  287. struct inode *inode = desc->file->f_path.dentry->d_inode;
  288. struct page *page;
  289. int status;
  290. dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
  291. __func__, desc->page_index,
  292. (long long) *desc->dir_cookie);
  293. /* If we find the page in the page_cache, we cannot be sure
  294. * how fresh the data is, so we will ignore readdir_plus attributes.
  295. */
  296. desc->timestamp_valid = 0;
  297. page = read_cache_page(inode->i_mapping, desc->page_index,
  298. (filler_t *)nfs_readdir_filler, desc);
  299. if (IS_ERR(page)) {
  300. status = PTR_ERR(page);
  301. goto out;
  302. }
  303. /* NOTE: Someone else may have changed the READDIRPLUS flag */
  304. desc->page = page;
  305. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  306. if (*desc->dir_cookie != 0)
  307. status = find_dirent(desc);
  308. else
  309. status = find_dirent_index(desc);
  310. if (status < 0)
  311. dir_page_release(desc);
  312. out:
  313. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
  314. return status;
  315. }
  316. /*
  317. * Recurse through the page cache pages, and return a
  318. * filled nfs_entry structure of the next directory entry if possible.
  319. *
  320. * The target for the search is '*desc->dir_cookie' if non-0,
  321. * 'desc->file->f_pos' otherwise
  322. */
  323. static inline
  324. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  325. {
  326. int loop_count = 0;
  327. int res;
  328. /* Always search-by-index from the beginning of the cache */
  329. if (*desc->dir_cookie == 0) {
  330. dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
  331. (long long)desc->file->f_pos);
  332. desc->page_index = 0;
  333. desc->entry->cookie = desc->entry->prev_cookie = 0;
  334. desc->entry->eof = 0;
  335. desc->current_index = 0;
  336. } else
  337. dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
  338. (unsigned long long)*desc->dir_cookie);
  339. for (;;) {
  340. res = find_dirent_page(desc);
  341. if (res != -EAGAIN)
  342. break;
  343. /* Align to beginning of next page */
  344. desc->page_index ++;
  345. if (loop_count++ > 200) {
  346. loop_count = 0;
  347. schedule();
  348. }
  349. }
  350. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, res);
  351. return res;
  352. }
  353. static inline unsigned int dt_type(struct inode *inode)
  354. {
  355. return (inode->i_mode >> 12) & 15;
  356. }
  357. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
  358. /*
  359. * Once we've found the start of the dirent within a page: fill 'er up...
  360. */
  361. static
  362. int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
  363. filldir_t filldir)
  364. {
  365. struct file *file = desc->file;
  366. struct nfs_entry *entry = desc->entry;
  367. struct dentry *dentry = NULL;
  368. u64 fileid;
  369. int loop_count = 0,
  370. res;
  371. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
  372. (unsigned long long)entry->cookie);
  373. for(;;) {
  374. unsigned d_type = DT_UNKNOWN;
  375. /* Note: entry->prev_cookie contains the cookie for
  376. * retrieving the current dirent on the server */
  377. fileid = entry->ino;
  378. /* Get a dentry if we have one */
  379. if (dentry != NULL)
  380. dput(dentry);
  381. dentry = nfs_readdir_lookup(desc);
  382. /* Use readdirplus info */
  383. if (dentry != NULL && dentry->d_inode != NULL) {
  384. d_type = dt_type(dentry->d_inode);
  385. fileid = NFS_FILEID(dentry->d_inode);
  386. }
  387. res = filldir(dirent, entry->name, entry->len,
  388. file->f_pos, nfs_compat_user_ino64(fileid),
  389. d_type);
  390. if (res < 0)
  391. break;
  392. file->f_pos++;
  393. *desc->dir_cookie = entry->cookie;
  394. if (dir_decode(desc) != 0) {
  395. desc->page_index ++;
  396. break;
  397. }
  398. if (loop_count++ > 200) {
  399. loop_count = 0;
  400. schedule();
  401. }
  402. }
  403. dir_page_release(desc);
  404. if (dentry != NULL)
  405. dput(dentry);
  406. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
  407. (unsigned long long)*desc->dir_cookie, res);
  408. return res;
  409. }
  410. /*
  411. * If we cannot find a cookie in our cache, we suspect that this is
  412. * because it points to a deleted file, so we ask the server to return
  413. * whatever it thinks is the next entry. We then feed this to filldir.
  414. * If all goes well, we should then be able to find our way round the
  415. * cache on the next call to readdir_search_pagecache();
  416. *
  417. * NOTE: we cannot add the anonymous page to the pagecache because
  418. * the data it contains might not be page aligned. Besides,
  419. * we should already have a complete representation of the
  420. * directory in the page cache by the time we get here.
  421. */
  422. static inline
  423. int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
  424. filldir_t filldir)
  425. {
  426. struct file *file = desc->file;
  427. struct inode *inode = file->f_path.dentry->d_inode;
  428. struct rpc_cred *cred = nfs_file_cred(file);
  429. struct page *page = NULL;
  430. int status;
  431. unsigned long timestamp;
  432. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
  433. (unsigned long long)*desc->dir_cookie);
  434. page = alloc_page(GFP_HIGHUSER);
  435. if (!page) {
  436. status = -ENOMEM;
  437. goto out;
  438. }
  439. timestamp = jiffies;
  440. status = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred,
  441. *desc->dir_cookie, page,
  442. NFS_SERVER(inode)->dtsize,
  443. desc->plus);
  444. desc->page = page;
  445. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  446. if (status >= 0) {
  447. desc->timestamp = timestamp;
  448. desc->timestamp_valid = 1;
  449. if ((status = dir_decode(desc)) == 0)
  450. desc->entry->prev_cookie = *desc->dir_cookie;
  451. } else
  452. status = -EIO;
  453. if (status < 0)
  454. goto out_release;
  455. status = nfs_do_filldir(desc, dirent, filldir);
  456. /* Reset read descriptor so it searches the page cache from
  457. * the start upon the next call to readdir_search_pagecache() */
  458. desc->page_index = 0;
  459. desc->entry->cookie = desc->entry->prev_cookie = 0;
  460. desc->entry->eof = 0;
  461. out:
  462. dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
  463. __func__, status);
  464. return status;
  465. out_release:
  466. dir_page_release(desc);
  467. goto out;
  468. }
  469. /* The file offset position represents the dirent entry number. A
  470. last cookie cache takes care of the common case of reading the
  471. whole directory.
  472. */
  473. static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  474. {
  475. struct dentry *dentry = filp->f_path.dentry;
  476. struct inode *inode = dentry->d_inode;
  477. nfs_readdir_descriptor_t my_desc,
  478. *desc = &my_desc;
  479. struct nfs_entry my_entry;
  480. struct nfs_fh fh;
  481. struct nfs_fattr fattr;
  482. long res;
  483. dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
  484. dentry->d_parent->d_name.name, dentry->d_name.name,
  485. (long long)filp->f_pos);
  486. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  487. /*
  488. * filp->f_pos points to the dirent entry number.
  489. * *desc->dir_cookie has the cookie for the next entry. We have
  490. * to either find the entry with the appropriate number or
  491. * revalidate the cookie.
  492. */
  493. memset(desc, 0, sizeof(*desc));
  494. desc->file = filp;
  495. desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
  496. desc->decode = NFS_PROTO(inode)->decode_dirent;
  497. desc->plus = NFS_USE_READDIRPLUS(inode);
  498. my_entry.cookie = my_entry.prev_cookie = 0;
  499. my_entry.eof = 0;
  500. my_entry.fh = &fh;
  501. my_entry.fattr = &fattr;
  502. nfs_fattr_init(&fattr);
  503. desc->entry = &my_entry;
  504. nfs_block_sillyrename(dentry);
  505. res = nfs_revalidate_mapping_nolock(inode, filp->f_mapping);
  506. if (res < 0)
  507. goto out;
  508. while(!desc->entry->eof) {
  509. res = readdir_search_pagecache(desc);
  510. if (res == -EBADCOOKIE) {
  511. /* This means either end of directory */
  512. if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
  513. /* Or that the server has 'lost' a cookie */
  514. res = uncached_readdir(desc, dirent, filldir);
  515. if (res >= 0)
  516. continue;
  517. }
  518. res = 0;
  519. break;
  520. }
  521. if (res == -ETOOSMALL && desc->plus) {
  522. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  523. nfs_zap_caches(inode);
  524. desc->plus = 0;
  525. desc->entry->eof = 0;
  526. continue;
  527. }
  528. if (res < 0)
  529. break;
  530. res = nfs_do_filldir(desc, dirent, filldir);
  531. if (res < 0) {
  532. res = 0;
  533. break;
  534. }
  535. }
  536. out:
  537. nfs_unblock_sillyrename(dentry);
  538. if (res > 0)
  539. res = 0;
  540. dfprintk(FILE, "NFS: readdir(%s/%s) returns %ld\n",
  541. dentry->d_parent->d_name.name, dentry->d_name.name,
  542. res);
  543. return res;
  544. }
  545. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
  546. {
  547. struct dentry *dentry = filp->f_path.dentry;
  548. struct inode *inode = dentry->d_inode;
  549. dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
  550. dentry->d_parent->d_name.name,
  551. dentry->d_name.name,
  552. offset, origin);
  553. mutex_lock(&inode->i_mutex);
  554. switch (origin) {
  555. case 1:
  556. offset += filp->f_pos;
  557. case 0:
  558. if (offset >= 0)
  559. break;
  560. default:
  561. offset = -EINVAL;
  562. goto out;
  563. }
  564. if (offset != filp->f_pos) {
  565. filp->f_pos = offset;
  566. nfs_file_open_context(filp)->dir_cookie = 0;
  567. }
  568. out:
  569. mutex_unlock(&inode->i_mutex);
  570. return offset;
  571. }
  572. /*
  573. * All directory operations under NFS are synchronous, so fsync()
  574. * is a dummy operation.
  575. */
  576. static int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
  577. {
  578. dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
  579. dentry->d_parent->d_name.name, dentry->d_name.name,
  580. datasync);
  581. nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
  582. return 0;
  583. }
  584. /**
  585. * nfs_force_lookup_revalidate - Mark the directory as having changed
  586. * @dir - pointer to directory inode
  587. *
  588. * This forces the revalidation code in nfs_lookup_revalidate() to do a
  589. * full lookup on all child dentries of 'dir' whenever a change occurs
  590. * on the server that might have invalidated our dcache.
  591. *
  592. * The caller should be holding dir->i_lock
  593. */
  594. void nfs_force_lookup_revalidate(struct inode *dir)
  595. {
  596. NFS_I(dir)->cache_change_attribute = jiffies;
  597. }
  598. /*
  599. * A check for whether or not the parent directory has changed.
  600. * In the case it has, we assume that the dentries are untrustworthy
  601. * and may need to be looked up again.
  602. */
  603. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  604. {
  605. if (IS_ROOT(dentry))
  606. return 1;
  607. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  608. return 0;
  609. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  610. return 0;
  611. /* Revalidate nfsi->cache_change_attribute before we declare a match */
  612. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  613. return 0;
  614. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  615. return 0;
  616. return 1;
  617. }
  618. /*
  619. * Return the intent data that applies to this particular path component
  620. *
  621. * Note that the current set of intents only apply to the very last
  622. * component of the path.
  623. * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
  624. */
  625. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
  626. {
  627. if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
  628. return 0;
  629. return nd->flags & mask;
  630. }
  631. /*
  632. * Use intent information to check whether or not we're going to do
  633. * an O_EXCL create using this path component.
  634. */
  635. static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  636. {
  637. if (NFS_PROTO(dir)->version == 2)
  638. return 0;
  639. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
  640. return 0;
  641. return (nd->intent.open.flags & O_EXCL) != 0;
  642. }
  643. /*
  644. * Inode and filehandle revalidation for lookups.
  645. *
  646. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  647. * or if the intent information indicates that we're about to open this
  648. * particular file and the "nocto" mount flag is not set.
  649. *
  650. */
  651. static inline
  652. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  653. {
  654. struct nfs_server *server = NFS_SERVER(inode);
  655. if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
  656. return 0;
  657. if (nd != NULL) {
  658. /* VFS wants an on-the-wire revalidation */
  659. if (nd->flags & LOOKUP_REVAL)
  660. goto out_force;
  661. /* This is an open(2) */
  662. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  663. !(server->flags & NFS_MOUNT_NOCTO) &&
  664. (S_ISREG(inode->i_mode) ||
  665. S_ISDIR(inode->i_mode)))
  666. goto out_force;
  667. return 0;
  668. }
  669. return nfs_revalidate_inode(server, inode);
  670. out_force:
  671. return __nfs_revalidate_inode(server, inode);
  672. }
  673. /*
  674. * We judge how long we want to trust negative
  675. * dentries by looking at the parent inode mtime.
  676. *
  677. * If parent mtime has changed, we revalidate, else we wait for a
  678. * period corresponding to the parent's attribute cache timeout value.
  679. */
  680. static inline
  681. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  682. struct nameidata *nd)
  683. {
  684. /* Don't revalidate a negative dentry if we're creating a new file */
  685. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  686. return 0;
  687. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  688. return 1;
  689. return !nfs_check_verifier(dir, dentry);
  690. }
  691. /*
  692. * This is called every time the dcache has a lookup hit,
  693. * and we should check whether we can really trust that
  694. * lookup.
  695. *
  696. * NOTE! The hit can be a negative hit too, don't assume
  697. * we have an inode!
  698. *
  699. * If the parent directory is seen to have changed, we throw out the
  700. * cached dentry and do a new lookup.
  701. */
  702. static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
  703. {
  704. struct inode *dir;
  705. struct inode *inode;
  706. struct dentry *parent;
  707. int error;
  708. struct nfs_fh fhandle;
  709. struct nfs_fattr fattr;
  710. parent = dget_parent(dentry);
  711. dir = parent->d_inode;
  712. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  713. inode = dentry->d_inode;
  714. if (!inode) {
  715. if (nfs_neg_need_reval(dir, dentry, nd))
  716. goto out_bad;
  717. goto out_valid;
  718. }
  719. if (is_bad_inode(inode)) {
  720. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  721. __func__, dentry->d_parent->d_name.name,
  722. dentry->d_name.name);
  723. goto out_bad;
  724. }
  725. /* Force a full look up iff the parent directory has changed */
  726. if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
  727. if (nfs_lookup_verify_inode(inode, nd))
  728. goto out_zap_parent;
  729. goto out_valid;
  730. }
  731. if (NFS_STALE(inode))
  732. goto out_bad;
  733. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  734. if (error)
  735. goto out_bad;
  736. if (nfs_compare_fh(NFS_FH(inode), &fhandle))
  737. goto out_bad;
  738. if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
  739. goto out_bad;
  740. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  741. out_valid:
  742. dput(parent);
  743. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  744. __func__, dentry->d_parent->d_name.name,
  745. dentry->d_name.name);
  746. return 1;
  747. out_zap_parent:
  748. nfs_zap_caches(dir);
  749. out_bad:
  750. nfs_mark_for_revalidate(dir);
  751. if (inode && S_ISDIR(inode->i_mode)) {
  752. /* Purge readdir caches. */
  753. nfs_zap_caches(inode);
  754. /* If we have submounts, don't unhash ! */
  755. if (have_submounts(dentry))
  756. goto out_valid;
  757. shrink_dcache_parent(dentry);
  758. }
  759. d_drop(dentry);
  760. dput(parent);
  761. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  762. __func__, dentry->d_parent->d_name.name,
  763. dentry->d_name.name);
  764. return 0;
  765. }
  766. /*
  767. * This is called from dput() when d_count is going to 0.
  768. */
  769. static int nfs_dentry_delete(struct dentry *dentry)
  770. {
  771. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  772. dentry->d_parent->d_name.name, dentry->d_name.name,
  773. dentry->d_flags);
  774. /* Unhash any dentry with a stale inode */
  775. if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
  776. return 1;
  777. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  778. /* Unhash it, so that ->d_iput() would be called */
  779. return 1;
  780. }
  781. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  782. /* Unhash it, so that ancestors of killed async unlink
  783. * files will be cleaned up during umount */
  784. return 1;
  785. }
  786. return 0;
  787. }
  788. static void nfs_drop_nlink(struct inode *inode)
  789. {
  790. spin_lock(&inode->i_lock);
  791. if (inode->i_nlink > 0)
  792. drop_nlink(inode);
  793. spin_unlock(&inode->i_lock);
  794. }
  795. /*
  796. * Called when the dentry loses inode.
  797. * We use it to clean up silly-renamed files.
  798. */
  799. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  800. {
  801. if (S_ISDIR(inode->i_mode))
  802. /* drop any readdir cache as it could easily be old */
  803. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  804. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  805. drop_nlink(inode);
  806. nfs_complete_unlink(dentry, inode);
  807. }
  808. iput(inode);
  809. }
  810. struct dentry_operations nfs_dentry_operations = {
  811. .d_revalidate = nfs_lookup_revalidate,
  812. .d_delete = nfs_dentry_delete,
  813. .d_iput = nfs_dentry_iput,
  814. };
  815. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  816. {
  817. struct dentry *res;
  818. struct dentry *parent;
  819. struct inode *inode = NULL;
  820. int error;
  821. struct nfs_fh fhandle;
  822. struct nfs_fattr fattr;
  823. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  824. dentry->d_parent->d_name.name, dentry->d_name.name);
  825. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  826. res = ERR_PTR(-ENAMETOOLONG);
  827. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  828. goto out;
  829. res = ERR_PTR(-ENOMEM);
  830. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  831. /*
  832. * If we're doing an exclusive create, optimize away the lookup
  833. * but don't hash the dentry.
  834. */
  835. if (nfs_is_exclusive_create(dir, nd)) {
  836. d_instantiate(dentry, NULL);
  837. res = NULL;
  838. goto out;
  839. }
  840. parent = dentry->d_parent;
  841. /* Protect against concurrent sillydeletes */
  842. nfs_block_sillyrename(parent);
  843. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  844. if (error == -ENOENT)
  845. goto no_entry;
  846. if (error < 0) {
  847. res = ERR_PTR(error);
  848. goto out_unblock_sillyrename;
  849. }
  850. inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
  851. res = (struct dentry *)inode;
  852. if (IS_ERR(res))
  853. goto out_unblock_sillyrename;
  854. no_entry:
  855. res = d_materialise_unique(dentry, inode);
  856. if (res != NULL) {
  857. if (IS_ERR(res))
  858. goto out_unblock_sillyrename;
  859. dentry = res;
  860. }
  861. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  862. out_unblock_sillyrename:
  863. nfs_unblock_sillyrename(parent);
  864. out:
  865. return res;
  866. }
  867. #ifdef CONFIG_NFS_V4
  868. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  869. struct dentry_operations nfs4_dentry_operations = {
  870. .d_revalidate = nfs_open_revalidate,
  871. .d_delete = nfs_dentry_delete,
  872. .d_iput = nfs_dentry_iput,
  873. };
  874. /*
  875. * Use intent information to determine whether we need to substitute
  876. * the NFSv4-style stateful OPEN for the LOOKUP call
  877. */
  878. static int is_atomic_open(struct inode *dir, struct nameidata *nd)
  879. {
  880. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  881. return 0;
  882. /* NFS does not (yet) have a stateful open for directories */
  883. if (nd->flags & LOOKUP_DIRECTORY)
  884. return 0;
  885. /* Are we trying to write to a read only partition? */
  886. if (__mnt_is_readonly(nd->path.mnt) &&
  887. (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
  888. return 0;
  889. return 1;
  890. }
  891. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  892. {
  893. struct dentry *res = NULL;
  894. int error;
  895. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  896. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  897. /* Check that we are indeed trying to open this file */
  898. if (!is_atomic_open(dir, nd))
  899. goto no_open;
  900. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  901. res = ERR_PTR(-ENAMETOOLONG);
  902. goto out;
  903. }
  904. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  905. /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
  906. * the dentry. */
  907. if (nd->intent.open.flags & O_EXCL) {
  908. d_instantiate(dentry, NULL);
  909. goto out;
  910. }
  911. /* Open the file on the server */
  912. res = nfs4_atomic_open(dir, dentry, nd);
  913. if (IS_ERR(res)) {
  914. error = PTR_ERR(res);
  915. switch (error) {
  916. /* Make a negative dentry */
  917. case -ENOENT:
  918. res = NULL;
  919. goto out;
  920. /* This turned out not to be a regular file */
  921. case -EISDIR:
  922. case -ENOTDIR:
  923. goto no_open;
  924. case -ELOOP:
  925. if (!(nd->intent.open.flags & O_NOFOLLOW))
  926. goto no_open;
  927. /* case -EINVAL: */
  928. default:
  929. goto out;
  930. }
  931. } else if (res != NULL)
  932. dentry = res;
  933. out:
  934. return res;
  935. no_open:
  936. return nfs_lookup(dir, dentry, nd);
  937. }
  938. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  939. {
  940. struct dentry *parent = NULL;
  941. struct inode *inode = dentry->d_inode;
  942. struct inode *dir;
  943. int openflags, ret = 0;
  944. parent = dget_parent(dentry);
  945. dir = parent->d_inode;
  946. if (!is_atomic_open(dir, nd))
  947. goto no_open;
  948. /* We can't create new files in nfs_open_revalidate(), so we
  949. * optimize away revalidation of negative dentries.
  950. */
  951. if (inode == NULL) {
  952. if (!nfs_neg_need_reval(dir, dentry, nd))
  953. ret = 1;
  954. goto out;
  955. }
  956. /* NFS only supports OPEN on regular files */
  957. if (!S_ISREG(inode->i_mode))
  958. goto no_open;
  959. openflags = nd->intent.open.flags;
  960. /* We cannot do exclusive creation on a positive dentry */
  961. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  962. goto no_open;
  963. /* We can't create new files, or truncate existing ones here */
  964. openflags &= ~(O_CREAT|O_TRUNC);
  965. /*
  966. * Note: we're not holding inode->i_mutex and so may be racing with
  967. * operations that change the directory. We therefore save the
  968. * change attribute *before* we do the RPC call.
  969. */
  970. ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
  971. out:
  972. dput(parent);
  973. if (!ret)
  974. d_drop(dentry);
  975. return ret;
  976. no_open:
  977. dput(parent);
  978. if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
  979. return 1;
  980. return nfs_lookup_revalidate(dentry, nd);
  981. }
  982. #endif /* CONFIG_NFSV4 */
  983. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
  984. {
  985. struct dentry *parent = desc->file->f_path.dentry;
  986. struct inode *dir = parent->d_inode;
  987. struct nfs_entry *entry = desc->entry;
  988. struct dentry *dentry, *alias;
  989. struct qstr name = {
  990. .name = entry->name,
  991. .len = entry->len,
  992. };
  993. struct inode *inode;
  994. unsigned long verf = nfs_save_change_attribute(dir);
  995. switch (name.len) {
  996. case 2:
  997. if (name.name[0] == '.' && name.name[1] == '.')
  998. return dget_parent(parent);
  999. break;
  1000. case 1:
  1001. if (name.name[0] == '.')
  1002. return dget(parent);
  1003. }
  1004. spin_lock(&dir->i_lock);
  1005. if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) {
  1006. spin_unlock(&dir->i_lock);
  1007. return NULL;
  1008. }
  1009. spin_unlock(&dir->i_lock);
  1010. name.hash = full_name_hash(name.name, name.len);
  1011. dentry = d_lookup(parent, &name);
  1012. if (dentry != NULL) {
  1013. /* Is this a positive dentry that matches the readdir info? */
  1014. if (dentry->d_inode != NULL &&
  1015. (NFS_FILEID(dentry->d_inode) == entry->ino ||
  1016. d_mountpoint(dentry))) {
  1017. if (!desc->plus || entry->fh->size == 0)
  1018. return dentry;
  1019. if (nfs_compare_fh(NFS_FH(dentry->d_inode),
  1020. entry->fh) == 0)
  1021. goto out_renew;
  1022. }
  1023. /* No, so d_drop to allow one to be created */
  1024. d_drop(dentry);
  1025. dput(dentry);
  1026. }
  1027. if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
  1028. return NULL;
  1029. if (name.len > NFS_SERVER(dir)->namelen)
  1030. return NULL;
  1031. /* Note: caller is already holding the dir->i_mutex! */
  1032. dentry = d_alloc(parent, &name);
  1033. if (dentry == NULL)
  1034. return NULL;
  1035. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  1036. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  1037. if (IS_ERR(inode)) {
  1038. dput(dentry);
  1039. return NULL;
  1040. }
  1041. alias = d_materialise_unique(dentry, inode);
  1042. if (alias != NULL) {
  1043. dput(dentry);
  1044. if (IS_ERR(alias))
  1045. return NULL;
  1046. dentry = alias;
  1047. }
  1048. out_renew:
  1049. nfs_set_verifier(dentry, verf);
  1050. return dentry;
  1051. }
  1052. /*
  1053. * Code common to create, mkdir, and mknod.
  1054. */
  1055. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1056. struct nfs_fattr *fattr)
  1057. {
  1058. struct dentry *parent = dget_parent(dentry);
  1059. struct inode *dir = parent->d_inode;
  1060. struct inode *inode;
  1061. int error = -EACCES;
  1062. d_drop(dentry);
  1063. /* We may have been initialized further down */
  1064. if (dentry->d_inode)
  1065. goto out;
  1066. if (fhandle->size == 0) {
  1067. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1068. if (error)
  1069. goto out_error;
  1070. }
  1071. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1072. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1073. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1074. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1075. if (error < 0)
  1076. goto out_error;
  1077. }
  1078. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1079. error = PTR_ERR(inode);
  1080. if (IS_ERR(inode))
  1081. goto out_error;
  1082. d_add(dentry, inode);
  1083. out:
  1084. dput(parent);
  1085. return 0;
  1086. out_error:
  1087. nfs_mark_for_revalidate(dir);
  1088. dput(parent);
  1089. return error;
  1090. }
  1091. /*
  1092. * Following a failed create operation, we drop the dentry rather
  1093. * than retain a negative dentry. This avoids a problem in the event
  1094. * that the operation succeeded on the server, but an error in the
  1095. * reply path made it appear to have failed.
  1096. */
  1097. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1098. struct nameidata *nd)
  1099. {
  1100. struct iattr attr;
  1101. int error;
  1102. int open_flags = 0;
  1103. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1104. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1105. attr.ia_mode = mode;
  1106. attr.ia_valid = ATTR_MODE;
  1107. if ((nd->flags & LOOKUP_CREATE) != 0)
  1108. open_flags = nd->intent.open.flags;
  1109. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
  1110. if (error != 0)
  1111. goto out_err;
  1112. return 0;
  1113. out_err:
  1114. d_drop(dentry);
  1115. return error;
  1116. }
  1117. /*
  1118. * See comments for nfs_proc_create regarding failed operations.
  1119. */
  1120. static int
  1121. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1122. {
  1123. struct iattr attr;
  1124. int status;
  1125. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1126. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1127. if (!new_valid_dev(rdev))
  1128. return -EINVAL;
  1129. attr.ia_mode = mode;
  1130. attr.ia_valid = ATTR_MODE;
  1131. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1132. if (status != 0)
  1133. goto out_err;
  1134. return 0;
  1135. out_err:
  1136. d_drop(dentry);
  1137. return status;
  1138. }
  1139. /*
  1140. * See comments for nfs_proc_create regarding failed operations.
  1141. */
  1142. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1143. {
  1144. struct iattr attr;
  1145. int error;
  1146. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1147. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1148. attr.ia_valid = ATTR_MODE;
  1149. attr.ia_mode = mode | S_IFDIR;
  1150. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1151. if (error != 0)
  1152. goto out_err;
  1153. return 0;
  1154. out_err:
  1155. d_drop(dentry);
  1156. return error;
  1157. }
  1158. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1159. {
  1160. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1161. d_delete(dentry);
  1162. }
  1163. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1164. {
  1165. int error;
  1166. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1167. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1168. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1169. /* Ensure the VFS deletes this inode */
  1170. if (error == 0 && dentry->d_inode != NULL)
  1171. clear_nlink(dentry->d_inode);
  1172. else if (error == -ENOENT)
  1173. nfs_dentry_handle_enoent(dentry);
  1174. return error;
  1175. }
  1176. static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  1177. {
  1178. static unsigned int sillycounter;
  1179. const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
  1180. const int countersize = sizeof(sillycounter)*2;
  1181. const int slen = sizeof(".nfs")+fileidsize+countersize-1;
  1182. char silly[slen+1];
  1183. struct qstr qsilly;
  1184. struct dentry *sdentry;
  1185. int error = -EIO;
  1186. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  1187. dentry->d_parent->d_name.name, dentry->d_name.name,
  1188. atomic_read(&dentry->d_count));
  1189. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  1190. /*
  1191. * We don't allow a dentry to be silly-renamed twice.
  1192. */
  1193. error = -EBUSY;
  1194. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1195. goto out;
  1196. sprintf(silly, ".nfs%*.*Lx",
  1197. fileidsize, fileidsize,
  1198. (unsigned long long)NFS_FILEID(dentry->d_inode));
  1199. /* Return delegation in anticipation of the rename */
  1200. nfs_inode_return_delegation(dentry->d_inode);
  1201. sdentry = NULL;
  1202. do {
  1203. char *suffix = silly + slen - countersize;
  1204. dput(sdentry);
  1205. sillycounter++;
  1206. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  1207. dfprintk(VFS, "NFS: trying to rename %s to %s\n",
  1208. dentry->d_name.name, silly);
  1209. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  1210. /*
  1211. * N.B. Better to return EBUSY here ... it could be
  1212. * dangerous to delete the file while it's in use.
  1213. */
  1214. if (IS_ERR(sdentry))
  1215. goto out;
  1216. } while(sdentry->d_inode != NULL); /* need negative lookup */
  1217. qsilly.name = silly;
  1218. qsilly.len = strlen(silly);
  1219. if (dentry->d_inode) {
  1220. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1221. dir, &qsilly);
  1222. nfs_mark_for_revalidate(dentry->d_inode);
  1223. } else
  1224. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1225. dir, &qsilly);
  1226. if (!error) {
  1227. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1228. d_move(dentry, sdentry);
  1229. error = nfs_async_unlink(dir, dentry);
  1230. /* If we return 0 we don't unlink */
  1231. }
  1232. dput(sdentry);
  1233. out:
  1234. return error;
  1235. }
  1236. /*
  1237. * Remove a file after making sure there are no pending writes,
  1238. * and after checking that the file has only one user.
  1239. *
  1240. * We invalidate the attribute cache and free the inode prior to the operation
  1241. * to avoid possible races if the server reuses the inode.
  1242. */
  1243. static int nfs_safe_remove(struct dentry *dentry)
  1244. {
  1245. struct inode *dir = dentry->d_parent->d_inode;
  1246. struct inode *inode = dentry->d_inode;
  1247. int error = -EBUSY;
  1248. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1249. dentry->d_parent->d_name.name, dentry->d_name.name);
  1250. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1251. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1252. error = 0;
  1253. goto out;
  1254. }
  1255. if (inode != NULL) {
  1256. nfs_inode_return_delegation(inode);
  1257. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1258. /* The VFS may want to delete this inode */
  1259. if (error == 0)
  1260. nfs_drop_nlink(inode);
  1261. nfs_mark_for_revalidate(inode);
  1262. } else
  1263. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1264. if (error == -ENOENT)
  1265. nfs_dentry_handle_enoent(dentry);
  1266. out:
  1267. return error;
  1268. }
  1269. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1270. * belongs to an active ".nfs..." file and we return -EBUSY.
  1271. *
  1272. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1273. */
  1274. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1275. {
  1276. int error;
  1277. int need_rehash = 0;
  1278. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1279. dir->i_ino, dentry->d_name.name);
  1280. spin_lock(&dcache_lock);
  1281. spin_lock(&dentry->d_lock);
  1282. if (atomic_read(&dentry->d_count) > 1) {
  1283. spin_unlock(&dentry->d_lock);
  1284. spin_unlock(&dcache_lock);
  1285. /* Start asynchronous writeout of the inode */
  1286. write_inode_now(dentry->d_inode, 0);
  1287. error = nfs_sillyrename(dir, dentry);
  1288. return error;
  1289. }
  1290. if (!d_unhashed(dentry)) {
  1291. __d_drop(dentry);
  1292. need_rehash = 1;
  1293. }
  1294. spin_unlock(&dentry->d_lock);
  1295. spin_unlock(&dcache_lock);
  1296. error = nfs_safe_remove(dentry);
  1297. if (!error || error == -ENOENT) {
  1298. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1299. } else if (need_rehash)
  1300. d_rehash(dentry);
  1301. return error;
  1302. }
  1303. /*
  1304. * To create a symbolic link, most file systems instantiate a new inode,
  1305. * add a page to it containing the path, then write it out to the disk
  1306. * using prepare_write/commit_write.
  1307. *
  1308. * Unfortunately the NFS client can't create the in-core inode first
  1309. * because it needs a file handle to create an in-core inode (see
  1310. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1311. * symlink request has completed on the server.
  1312. *
  1313. * So instead we allocate a raw page, copy the symname into it, then do
  1314. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1315. * now have a new file handle and can instantiate an in-core NFS inode
  1316. * and move the raw page into its mapping.
  1317. */
  1318. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1319. {
  1320. struct pagevec lru_pvec;
  1321. struct page *page;
  1322. char *kaddr;
  1323. struct iattr attr;
  1324. unsigned int pathlen = strlen(symname);
  1325. int error;
  1326. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1327. dir->i_ino, dentry->d_name.name, symname);
  1328. if (pathlen > PAGE_SIZE)
  1329. return -ENAMETOOLONG;
  1330. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1331. attr.ia_valid = ATTR_MODE;
  1332. page = alloc_page(GFP_HIGHUSER);
  1333. if (!page)
  1334. return -ENOMEM;
  1335. kaddr = kmap_atomic(page, KM_USER0);
  1336. memcpy(kaddr, symname, pathlen);
  1337. if (pathlen < PAGE_SIZE)
  1338. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1339. kunmap_atomic(kaddr, KM_USER0);
  1340. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1341. if (error != 0) {
  1342. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1343. dir->i_sb->s_id, dir->i_ino,
  1344. dentry->d_name.name, symname, error);
  1345. d_drop(dentry);
  1346. __free_page(page);
  1347. return error;
  1348. }
  1349. /*
  1350. * No big deal if we can't add this page to the page cache here.
  1351. * READLINK will get the missing page from the server if needed.
  1352. */
  1353. pagevec_init(&lru_pvec, 0);
  1354. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1355. GFP_KERNEL)) {
  1356. pagevec_add(&lru_pvec, page);
  1357. pagevec_lru_add(&lru_pvec);
  1358. SetPageUptodate(page);
  1359. unlock_page(page);
  1360. } else
  1361. __free_page(page);
  1362. return 0;
  1363. }
  1364. static int
  1365. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1366. {
  1367. struct inode *inode = old_dentry->d_inode;
  1368. int error;
  1369. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1370. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1371. dentry->d_parent->d_name.name, dentry->d_name.name);
  1372. d_drop(dentry);
  1373. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1374. if (error == 0) {
  1375. atomic_inc(&inode->i_count);
  1376. d_add(dentry, inode);
  1377. }
  1378. return error;
  1379. }
  1380. /*
  1381. * RENAME
  1382. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1383. * different file handle for the same inode after a rename (e.g. when
  1384. * moving to a different directory). A fail-safe method to do so would
  1385. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1386. * rename the old file using the sillyrename stuff. This way, the original
  1387. * file in old_dir will go away when the last process iput()s the inode.
  1388. *
  1389. * FIXED.
  1390. *
  1391. * It actually works quite well. One needs to have the possibility for
  1392. * at least one ".nfs..." file in each directory the file ever gets
  1393. * moved or linked to which happens automagically with the new
  1394. * implementation that only depends on the dcache stuff instead of
  1395. * using the inode layer
  1396. *
  1397. * Unfortunately, things are a little more complicated than indicated
  1398. * above. For a cross-directory move, we want to make sure we can get
  1399. * rid of the old inode after the operation. This means there must be
  1400. * no pending writes (if it's a file), and the use count must be 1.
  1401. * If these conditions are met, we can drop the dentries before doing
  1402. * the rename.
  1403. */
  1404. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1405. struct inode *new_dir, struct dentry *new_dentry)
  1406. {
  1407. struct inode *old_inode = old_dentry->d_inode;
  1408. struct inode *new_inode = new_dentry->d_inode;
  1409. struct dentry *dentry = NULL, *rehash = NULL;
  1410. int error = -EBUSY;
  1411. /*
  1412. * To prevent any new references to the target during the rename,
  1413. * we unhash the dentry and free the inode in advance.
  1414. */
  1415. if (!d_unhashed(new_dentry)) {
  1416. d_drop(new_dentry);
  1417. rehash = new_dentry;
  1418. }
  1419. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1420. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1421. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1422. atomic_read(&new_dentry->d_count));
  1423. /*
  1424. * First check whether the target is busy ... we can't
  1425. * safely do _any_ rename if the target is in use.
  1426. *
  1427. * For files, make a copy of the dentry and then do a
  1428. * silly-rename. If the silly-rename succeeds, the
  1429. * copied dentry is hashed and becomes the new target.
  1430. */
  1431. if (!new_inode)
  1432. goto go_ahead;
  1433. if (S_ISDIR(new_inode->i_mode)) {
  1434. error = -EISDIR;
  1435. if (!S_ISDIR(old_inode->i_mode))
  1436. goto out;
  1437. } else if (atomic_read(&new_dentry->d_count) > 2) {
  1438. int err;
  1439. /* copy the target dentry's name */
  1440. dentry = d_alloc(new_dentry->d_parent,
  1441. &new_dentry->d_name);
  1442. if (!dentry)
  1443. goto out;
  1444. /* silly-rename the existing target ... */
  1445. err = nfs_sillyrename(new_dir, new_dentry);
  1446. if (!err) {
  1447. new_dentry = rehash = dentry;
  1448. new_inode = NULL;
  1449. /* instantiate the replacement target */
  1450. d_instantiate(new_dentry, NULL);
  1451. } else if (atomic_read(&new_dentry->d_count) > 1)
  1452. /* dentry still busy? */
  1453. goto out;
  1454. } else
  1455. nfs_drop_nlink(new_inode);
  1456. go_ahead:
  1457. /*
  1458. * ... prune child dentries and writebacks if needed.
  1459. */
  1460. if (atomic_read(&old_dentry->d_count) > 1) {
  1461. if (S_ISREG(old_inode->i_mode))
  1462. nfs_wb_all(old_inode);
  1463. shrink_dcache_parent(old_dentry);
  1464. }
  1465. nfs_inode_return_delegation(old_inode);
  1466. if (new_inode != NULL) {
  1467. nfs_inode_return_delegation(new_inode);
  1468. d_delete(new_dentry);
  1469. }
  1470. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1471. new_dir, &new_dentry->d_name);
  1472. nfs_mark_for_revalidate(old_inode);
  1473. out:
  1474. if (rehash)
  1475. d_rehash(rehash);
  1476. if (!error) {
  1477. d_move(old_dentry, new_dentry);
  1478. nfs_set_verifier(new_dentry,
  1479. nfs_save_change_attribute(new_dir));
  1480. } else if (error == -ENOENT)
  1481. nfs_dentry_handle_enoent(old_dentry);
  1482. /* new dentry created? */
  1483. if (dentry)
  1484. dput(dentry);
  1485. return error;
  1486. }
  1487. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1488. static LIST_HEAD(nfs_access_lru_list);
  1489. static atomic_long_t nfs_access_nr_entries;
  1490. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1491. {
  1492. put_rpccred(entry->cred);
  1493. kfree(entry);
  1494. smp_mb__before_atomic_dec();
  1495. atomic_long_dec(&nfs_access_nr_entries);
  1496. smp_mb__after_atomic_dec();
  1497. }
  1498. int nfs_access_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
  1499. {
  1500. LIST_HEAD(head);
  1501. struct nfs_inode *nfsi;
  1502. struct nfs_access_entry *cache;
  1503. restart:
  1504. spin_lock(&nfs_access_lru_lock);
  1505. list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
  1506. struct rw_semaphore *s_umount;
  1507. struct inode *inode;
  1508. if (nr_to_scan-- == 0)
  1509. break;
  1510. s_umount = &nfsi->vfs_inode.i_sb->s_umount;
  1511. if (!down_read_trylock(s_umount))
  1512. continue;
  1513. inode = igrab(&nfsi->vfs_inode);
  1514. if (inode == NULL) {
  1515. up_read(s_umount);
  1516. continue;
  1517. }
  1518. spin_lock(&inode->i_lock);
  1519. if (list_empty(&nfsi->access_cache_entry_lru))
  1520. goto remove_lru_entry;
  1521. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1522. struct nfs_access_entry, lru);
  1523. list_move(&cache->lru, &head);
  1524. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1525. if (!list_empty(&nfsi->access_cache_entry_lru))
  1526. list_move_tail(&nfsi->access_cache_inode_lru,
  1527. &nfs_access_lru_list);
  1528. else {
  1529. remove_lru_entry:
  1530. list_del_init(&nfsi->access_cache_inode_lru);
  1531. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1532. }
  1533. spin_unlock(&inode->i_lock);
  1534. spin_unlock(&nfs_access_lru_lock);
  1535. iput(inode);
  1536. up_read(s_umount);
  1537. goto restart;
  1538. }
  1539. spin_unlock(&nfs_access_lru_lock);
  1540. while (!list_empty(&head)) {
  1541. cache = list_entry(head.next, struct nfs_access_entry, lru);
  1542. list_del(&cache->lru);
  1543. nfs_access_free_entry(cache);
  1544. }
  1545. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1546. }
  1547. static void __nfs_access_zap_cache(struct inode *inode)
  1548. {
  1549. struct nfs_inode *nfsi = NFS_I(inode);
  1550. struct rb_root *root_node = &nfsi->access_cache;
  1551. struct rb_node *n, *dispose = NULL;
  1552. struct nfs_access_entry *entry;
  1553. /* Unhook entries from the cache */
  1554. while ((n = rb_first(root_node)) != NULL) {
  1555. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1556. rb_erase(n, root_node);
  1557. list_del(&entry->lru);
  1558. n->rb_left = dispose;
  1559. dispose = n;
  1560. }
  1561. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1562. spin_unlock(&inode->i_lock);
  1563. /* Now kill them all! */
  1564. while (dispose != NULL) {
  1565. n = dispose;
  1566. dispose = n->rb_left;
  1567. nfs_access_free_entry(rb_entry(n, struct nfs_access_entry, rb_node));
  1568. }
  1569. }
  1570. void nfs_access_zap_cache(struct inode *inode)
  1571. {
  1572. /* Remove from global LRU init */
  1573. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1574. spin_lock(&nfs_access_lru_lock);
  1575. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1576. spin_unlock(&nfs_access_lru_lock);
  1577. }
  1578. spin_lock(&inode->i_lock);
  1579. /* This will release the spinlock */
  1580. __nfs_access_zap_cache(inode);
  1581. }
  1582. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1583. {
  1584. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1585. struct nfs_access_entry *entry;
  1586. while (n != NULL) {
  1587. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1588. if (cred < entry->cred)
  1589. n = n->rb_left;
  1590. else if (cred > entry->cred)
  1591. n = n->rb_right;
  1592. else
  1593. return entry;
  1594. }
  1595. return NULL;
  1596. }
  1597. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1598. {
  1599. struct nfs_inode *nfsi = NFS_I(inode);
  1600. struct nfs_access_entry *cache;
  1601. int err = -ENOENT;
  1602. spin_lock(&inode->i_lock);
  1603. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1604. goto out_zap;
  1605. cache = nfs_access_search_rbtree(inode, cred);
  1606. if (cache == NULL)
  1607. goto out;
  1608. if (!time_in_range(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1609. goto out_stale;
  1610. res->jiffies = cache->jiffies;
  1611. res->cred = cache->cred;
  1612. res->mask = cache->mask;
  1613. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1614. err = 0;
  1615. out:
  1616. spin_unlock(&inode->i_lock);
  1617. return err;
  1618. out_stale:
  1619. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1620. list_del(&cache->lru);
  1621. spin_unlock(&inode->i_lock);
  1622. nfs_access_free_entry(cache);
  1623. return -ENOENT;
  1624. out_zap:
  1625. /* This will release the spinlock */
  1626. __nfs_access_zap_cache(inode);
  1627. return -ENOENT;
  1628. }
  1629. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1630. {
  1631. struct nfs_inode *nfsi = NFS_I(inode);
  1632. struct rb_root *root_node = &nfsi->access_cache;
  1633. struct rb_node **p = &root_node->rb_node;
  1634. struct rb_node *parent = NULL;
  1635. struct nfs_access_entry *entry;
  1636. spin_lock(&inode->i_lock);
  1637. while (*p != NULL) {
  1638. parent = *p;
  1639. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1640. if (set->cred < entry->cred)
  1641. p = &parent->rb_left;
  1642. else if (set->cred > entry->cred)
  1643. p = &parent->rb_right;
  1644. else
  1645. goto found;
  1646. }
  1647. rb_link_node(&set->rb_node, parent, p);
  1648. rb_insert_color(&set->rb_node, root_node);
  1649. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1650. spin_unlock(&inode->i_lock);
  1651. return;
  1652. found:
  1653. rb_replace_node(parent, &set->rb_node, root_node);
  1654. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1655. list_del(&entry->lru);
  1656. spin_unlock(&inode->i_lock);
  1657. nfs_access_free_entry(entry);
  1658. }
  1659. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1660. {
  1661. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1662. if (cache == NULL)
  1663. return;
  1664. RB_CLEAR_NODE(&cache->rb_node);
  1665. cache->jiffies = set->jiffies;
  1666. cache->cred = get_rpccred(set->cred);
  1667. cache->mask = set->mask;
  1668. nfs_access_add_rbtree(inode, cache);
  1669. /* Update accounting */
  1670. smp_mb__before_atomic_inc();
  1671. atomic_long_inc(&nfs_access_nr_entries);
  1672. smp_mb__after_atomic_inc();
  1673. /* Add inode to global LRU list */
  1674. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1675. spin_lock(&nfs_access_lru_lock);
  1676. list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
  1677. spin_unlock(&nfs_access_lru_lock);
  1678. }
  1679. }
  1680. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1681. {
  1682. struct nfs_access_entry cache;
  1683. int status;
  1684. status = nfs_access_get_cached(inode, cred, &cache);
  1685. if (status == 0)
  1686. goto out;
  1687. /* Be clever: ask server to check for all possible rights */
  1688. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1689. cache.cred = cred;
  1690. cache.jiffies = jiffies;
  1691. status = NFS_PROTO(inode)->access(inode, &cache);
  1692. if (status != 0)
  1693. return status;
  1694. nfs_access_add_cache(inode, &cache);
  1695. out:
  1696. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1697. return 0;
  1698. return -EACCES;
  1699. }
  1700. static int nfs_open_permission_mask(int openflags)
  1701. {
  1702. int mask = 0;
  1703. if (openflags & FMODE_READ)
  1704. mask |= MAY_READ;
  1705. if (openflags & FMODE_WRITE)
  1706. mask |= MAY_WRITE;
  1707. if (openflags & FMODE_EXEC)
  1708. mask |= MAY_EXEC;
  1709. return mask;
  1710. }
  1711. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  1712. {
  1713. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  1714. }
  1715. int nfs_permission(struct inode *inode, int mask)
  1716. {
  1717. struct rpc_cred *cred;
  1718. int res = 0;
  1719. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  1720. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1721. goto out;
  1722. /* Is this sys_access() ? */
  1723. if (mask & MAY_ACCESS)
  1724. goto force_lookup;
  1725. switch (inode->i_mode & S_IFMT) {
  1726. case S_IFLNK:
  1727. goto out;
  1728. case S_IFREG:
  1729. /* NFSv4 has atomic_open... */
  1730. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  1731. && (mask & MAY_OPEN))
  1732. goto out;
  1733. break;
  1734. case S_IFDIR:
  1735. /*
  1736. * Optimize away all write operations, since the server
  1737. * will check permissions when we perform the op.
  1738. */
  1739. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1740. goto out;
  1741. }
  1742. force_lookup:
  1743. if (!NFS_PROTO(inode)->access)
  1744. goto out_notsup;
  1745. cred = rpc_lookup_cred();
  1746. if (!IS_ERR(cred)) {
  1747. res = nfs_do_access(inode, cred, mask);
  1748. put_rpccred(cred);
  1749. } else
  1750. res = PTR_ERR(cred);
  1751. out:
  1752. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  1753. inode->i_sb->s_id, inode->i_ino, mask, res);
  1754. return res;
  1755. out_notsup:
  1756. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1757. if (res == 0)
  1758. res = generic_permission(inode, mask, NULL);
  1759. goto out;
  1760. }
  1761. /*
  1762. * Local variables:
  1763. * version-control: t
  1764. * kept-new-versions: 5
  1765. * End:
  1766. */