dir.c 54 KB

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