dir.c 44 KB

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