dir.c 59 KB

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