dir.c 52 KB

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