namei.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547
  1. /*
  2. * linux/fs/ext4/namei.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/namei.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. * Directory entry file type support and forward compatibility hooks
  18. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  19. * Hash Tree Directory indexing (c)
  20. * Daniel Phillips, 2001
  21. * Hash Tree Directory indexing porting
  22. * Christopher Li, 2002
  23. * Hash Tree Directory indexing cleanup
  24. * Theodore Ts'o, 2002
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/jbd2.h>
  29. #include <linux/time.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/quotaops.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/bio.h>
  36. #include "ext4.h"
  37. #include "ext4_jbd2.h"
  38. #include "xattr.h"
  39. #include "acl.h"
  40. /*
  41. * define how far ahead to read directories while searching them.
  42. */
  43. #define NAMEI_RA_CHUNKS 2
  44. #define NAMEI_RA_BLOCKS 4
  45. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  46. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  47. static struct buffer_head *ext4_append(handle_t *handle,
  48. struct inode *inode,
  49. ext4_lblk_t *block, int *err)
  50. {
  51. struct buffer_head *bh;
  52. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  53. bh = ext4_bread(handle, inode, *block, 1, err);
  54. if (bh) {
  55. inode->i_size += inode->i_sb->s_blocksize;
  56. EXT4_I(inode)->i_disksize = inode->i_size;
  57. *err = ext4_journal_get_write_access(handle, bh);
  58. if (*err) {
  59. brelse(bh);
  60. bh = NULL;
  61. }
  62. }
  63. return bh;
  64. }
  65. #ifndef assert
  66. #define assert(test) J_ASSERT(test)
  67. #endif
  68. #ifdef DX_DEBUG
  69. #define dxtrace(command) command
  70. #else
  71. #define dxtrace(command)
  72. #endif
  73. struct fake_dirent
  74. {
  75. __le32 inode;
  76. __le16 rec_len;
  77. u8 name_len;
  78. u8 file_type;
  79. };
  80. struct dx_countlimit
  81. {
  82. __le16 limit;
  83. __le16 count;
  84. };
  85. struct dx_entry
  86. {
  87. __le32 hash;
  88. __le32 block;
  89. };
  90. /*
  91. * dx_root_info is laid out so that if it should somehow get overlaid by a
  92. * dirent the two low bits of the hash version will be zero. Therefore, the
  93. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  94. */
  95. struct dx_root
  96. {
  97. struct fake_dirent dot;
  98. char dot_name[4];
  99. struct fake_dirent dotdot;
  100. char dotdot_name[4];
  101. struct dx_root_info
  102. {
  103. __le32 reserved_zero;
  104. u8 hash_version;
  105. u8 info_length; /* 8 */
  106. u8 indirect_levels;
  107. u8 unused_flags;
  108. }
  109. info;
  110. struct dx_entry entries[0];
  111. };
  112. struct dx_node
  113. {
  114. struct fake_dirent fake;
  115. struct dx_entry entries[0];
  116. };
  117. struct dx_frame
  118. {
  119. struct buffer_head *bh;
  120. struct dx_entry *entries;
  121. struct dx_entry *at;
  122. };
  123. struct dx_map_entry
  124. {
  125. u32 hash;
  126. u16 offs;
  127. u16 size;
  128. };
  129. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  130. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  131. static inline unsigned dx_get_hash(struct dx_entry *entry);
  132. static void dx_set_hash(struct dx_entry *entry, unsigned value);
  133. static unsigned dx_get_count(struct dx_entry *entries);
  134. static unsigned dx_get_limit(struct dx_entry *entries);
  135. static void dx_set_count(struct dx_entry *entries, unsigned value);
  136. static void dx_set_limit(struct dx_entry *entries, unsigned value);
  137. static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
  138. static unsigned dx_node_limit(struct inode *dir);
  139. static struct dx_frame *dx_probe(const struct qstr *d_name,
  140. struct inode *dir,
  141. struct dx_hash_info *hinfo,
  142. struct dx_frame *frame,
  143. int *err);
  144. static void dx_release(struct dx_frame *frames);
  145. static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
  146. struct dx_hash_info *hinfo, struct dx_map_entry map[]);
  147. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  148. static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
  149. struct dx_map_entry *offsets, int count, unsigned blocksize);
  150. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
  151. static void dx_insert_block(struct dx_frame *frame,
  152. u32 hash, ext4_lblk_t block);
  153. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  154. struct dx_frame *frame,
  155. struct dx_frame *frames,
  156. __u32 *start_hash);
  157. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  158. const struct qstr *d_name,
  159. struct ext4_dir_entry_2 **res_dir,
  160. int *err);
  161. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  162. struct inode *inode);
  163. /*
  164. * p is at least 6 bytes before the end of page
  165. */
  166. static inline struct ext4_dir_entry_2 *
  167. ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
  168. {
  169. return (struct ext4_dir_entry_2 *)((char *)p +
  170. ext4_rec_len_from_disk(p->rec_len, blocksize));
  171. }
  172. /*
  173. * Future: use high four bits of block for coalesce-on-delete flags
  174. * Mask them off for now.
  175. */
  176. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  177. {
  178. return le32_to_cpu(entry->block) & 0x00ffffff;
  179. }
  180. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  181. {
  182. entry->block = cpu_to_le32(value);
  183. }
  184. static inline unsigned dx_get_hash(struct dx_entry *entry)
  185. {
  186. return le32_to_cpu(entry->hash);
  187. }
  188. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  189. {
  190. entry->hash = cpu_to_le32(value);
  191. }
  192. static inline unsigned dx_get_count(struct dx_entry *entries)
  193. {
  194. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  195. }
  196. static inline unsigned dx_get_limit(struct dx_entry *entries)
  197. {
  198. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  199. }
  200. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  201. {
  202. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  203. }
  204. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  205. {
  206. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  207. }
  208. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  209. {
  210. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  211. EXT4_DIR_REC_LEN(2) - infosize;
  212. return entry_space / sizeof(struct dx_entry);
  213. }
  214. static inline unsigned dx_node_limit(struct inode *dir)
  215. {
  216. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  217. return entry_space / sizeof(struct dx_entry);
  218. }
  219. /*
  220. * Debug
  221. */
  222. #ifdef DX_DEBUG
  223. static void dx_show_index(char * label, struct dx_entry *entries)
  224. {
  225. int i, n = dx_get_count (entries);
  226. printk(KERN_DEBUG "%s index ", label);
  227. for (i = 0; i < n; i++) {
  228. printk("%x->%lu ", i ? dx_get_hash(entries + i) :
  229. 0, (unsigned long)dx_get_block(entries + i));
  230. }
  231. printk("\n");
  232. }
  233. struct stats
  234. {
  235. unsigned names;
  236. unsigned space;
  237. unsigned bcount;
  238. };
  239. static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
  240. int size, int show_names)
  241. {
  242. unsigned names = 0, space = 0;
  243. char *base = (char *) de;
  244. struct dx_hash_info h = *hinfo;
  245. printk("names: ");
  246. while ((char *) de < base + size)
  247. {
  248. if (de->inode)
  249. {
  250. if (show_names)
  251. {
  252. int len = de->name_len;
  253. char *name = de->name;
  254. while (len--) printk("%c", *name++);
  255. ext4fs_dirhash(de->name, de->name_len, &h);
  256. printk(":%x.%u ", h.hash,
  257. ((char *) de - base));
  258. }
  259. space += EXT4_DIR_REC_LEN(de->name_len);
  260. names++;
  261. }
  262. de = ext4_next_entry(de, size);
  263. }
  264. printk("(%i)\n", names);
  265. return (struct stats) { names, space, 1 };
  266. }
  267. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  268. struct dx_entry *entries, int levels)
  269. {
  270. unsigned blocksize = dir->i_sb->s_blocksize;
  271. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  272. unsigned bcount = 0;
  273. struct buffer_head *bh;
  274. int err;
  275. printk("%i indexed blocks...\n", count);
  276. for (i = 0; i < count; i++, entries++)
  277. {
  278. ext4_lblk_t block = dx_get_block(entries);
  279. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  280. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  281. struct stats stats;
  282. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  283. if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
  284. stats = levels?
  285. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  286. dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
  287. names += stats.names;
  288. space += stats.space;
  289. bcount += stats.bcount;
  290. brelse(bh);
  291. }
  292. if (bcount)
  293. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  294. levels ? "" : " ", names, space/bcount,
  295. (space/bcount)*100/blocksize);
  296. return (struct stats) { names, space, bcount};
  297. }
  298. #endif /* DX_DEBUG */
  299. /*
  300. * Probe for a directory leaf block to search.
  301. *
  302. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  303. * error in the directory index, and the caller should fall back to
  304. * searching the directory normally. The callers of dx_probe **MUST**
  305. * check for this error code, and make sure it never gets reflected
  306. * back to userspace.
  307. */
  308. static struct dx_frame *
  309. dx_probe(const struct qstr *d_name, struct inode *dir,
  310. struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
  311. {
  312. unsigned count, indirect;
  313. struct dx_entry *at, *entries, *p, *q, *m;
  314. struct dx_root *root;
  315. struct buffer_head *bh;
  316. struct dx_frame *frame = frame_in;
  317. u32 hash;
  318. frame->bh = NULL;
  319. if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
  320. goto fail;
  321. root = (struct dx_root *) bh->b_data;
  322. if (root->info.hash_version != DX_HASH_TEA &&
  323. root->info.hash_version != DX_HASH_HALF_MD4 &&
  324. root->info.hash_version != DX_HASH_LEGACY) {
  325. ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
  326. root->info.hash_version);
  327. brelse(bh);
  328. *err = ERR_BAD_DX_DIR;
  329. goto fail;
  330. }
  331. hinfo->hash_version = root->info.hash_version;
  332. if (hinfo->hash_version <= DX_HASH_TEA)
  333. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  334. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  335. if (d_name)
  336. ext4fs_dirhash(d_name->name, d_name->len, hinfo);
  337. hash = hinfo->hash;
  338. if (root->info.unused_flags & 1) {
  339. ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
  340. root->info.unused_flags);
  341. brelse(bh);
  342. *err = ERR_BAD_DX_DIR;
  343. goto fail;
  344. }
  345. if ((indirect = root->info.indirect_levels) > 1) {
  346. ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
  347. root->info.indirect_levels);
  348. brelse(bh);
  349. *err = ERR_BAD_DX_DIR;
  350. goto fail;
  351. }
  352. entries = (struct dx_entry *) (((char *)&root->info) +
  353. root->info.info_length);
  354. if (dx_get_limit(entries) != dx_root_limit(dir,
  355. root->info.info_length)) {
  356. ext4_warning(dir->i_sb, "dx entry: limit != root limit");
  357. brelse(bh);
  358. *err = ERR_BAD_DX_DIR;
  359. goto fail;
  360. }
  361. dxtrace(printk("Look up %x", hash));
  362. while (1)
  363. {
  364. count = dx_get_count(entries);
  365. if (!count || count > dx_get_limit(entries)) {
  366. ext4_warning(dir->i_sb,
  367. "dx entry: no count or count > limit");
  368. brelse(bh);
  369. *err = ERR_BAD_DX_DIR;
  370. goto fail2;
  371. }
  372. p = entries + 1;
  373. q = entries + count - 1;
  374. while (p <= q)
  375. {
  376. m = p + (q - p)/2;
  377. dxtrace(printk("."));
  378. if (dx_get_hash(m) > hash)
  379. q = m - 1;
  380. else
  381. p = m + 1;
  382. }
  383. if (0) // linear search cross check
  384. {
  385. unsigned n = count - 1;
  386. at = entries;
  387. while (n--)
  388. {
  389. dxtrace(printk(","));
  390. if (dx_get_hash(++at) > hash)
  391. {
  392. at--;
  393. break;
  394. }
  395. }
  396. assert (at == p - 1);
  397. }
  398. at = p - 1;
  399. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  400. frame->bh = bh;
  401. frame->entries = entries;
  402. frame->at = at;
  403. if (!indirect--) return frame;
  404. if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
  405. goto fail2;
  406. at = entries = ((struct dx_node *) bh->b_data)->entries;
  407. if (dx_get_limit(entries) != dx_node_limit (dir)) {
  408. ext4_warning(dir->i_sb,
  409. "dx entry: limit != node limit");
  410. brelse(bh);
  411. *err = ERR_BAD_DX_DIR;
  412. goto fail2;
  413. }
  414. frame++;
  415. frame->bh = NULL;
  416. }
  417. fail2:
  418. while (frame >= frame_in) {
  419. brelse(frame->bh);
  420. frame--;
  421. }
  422. fail:
  423. if (*err == ERR_BAD_DX_DIR)
  424. ext4_warning(dir->i_sb,
  425. "Corrupt dir inode %ld, running e2fsck is "
  426. "recommended.", dir->i_ino);
  427. return NULL;
  428. }
  429. static void dx_release (struct dx_frame *frames)
  430. {
  431. if (frames[0].bh == NULL)
  432. return;
  433. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  434. brelse(frames[1].bh);
  435. brelse(frames[0].bh);
  436. }
  437. /*
  438. * This function increments the frame pointer to search the next leaf
  439. * block, and reads in the necessary intervening nodes if the search
  440. * should be necessary. Whether or not the search is necessary is
  441. * controlled by the hash parameter. If the hash value is even, then
  442. * the search is only continued if the next block starts with that
  443. * hash value. This is used if we are searching for a specific file.
  444. *
  445. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  446. *
  447. * This function returns 1 if the caller should continue to search,
  448. * or 0 if it should not. If there is an error reading one of the
  449. * index blocks, it will a negative error code.
  450. *
  451. * If start_hash is non-null, it will be filled in with the starting
  452. * hash of the next page.
  453. */
  454. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  455. struct dx_frame *frame,
  456. struct dx_frame *frames,
  457. __u32 *start_hash)
  458. {
  459. struct dx_frame *p;
  460. struct buffer_head *bh;
  461. int err, num_frames = 0;
  462. __u32 bhash;
  463. p = frame;
  464. /*
  465. * Find the next leaf page by incrementing the frame pointer.
  466. * If we run out of entries in the interior node, loop around and
  467. * increment pointer in the parent node. When we break out of
  468. * this loop, num_frames indicates the number of interior
  469. * nodes need to be read.
  470. */
  471. while (1) {
  472. if (++(p->at) < p->entries + dx_get_count(p->entries))
  473. break;
  474. if (p == frames)
  475. return 0;
  476. num_frames++;
  477. p--;
  478. }
  479. /*
  480. * If the hash is 1, then continue only if the next page has a
  481. * continuation hash of any value. This is used for readdir
  482. * handling. Otherwise, check to see if the hash matches the
  483. * desired contiuation hash. If it doesn't, return since
  484. * there's no point to read in the successive index pages.
  485. */
  486. bhash = dx_get_hash(p->at);
  487. if (start_hash)
  488. *start_hash = bhash;
  489. if ((hash & 1) == 0) {
  490. if ((bhash & ~1) != hash)
  491. return 0;
  492. }
  493. /*
  494. * If the hash is HASH_NB_ALWAYS, we always go to the next
  495. * block so no check is necessary
  496. */
  497. while (num_frames--) {
  498. if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
  499. 0, &err)))
  500. return err; /* Failure */
  501. p++;
  502. brelse(p->bh);
  503. p->bh = bh;
  504. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  505. }
  506. return 1;
  507. }
  508. /*
  509. * This function fills a red-black tree with information from a
  510. * directory block. It returns the number directory entries loaded
  511. * into the tree. If there is an error it is returned in err.
  512. */
  513. static int htree_dirblock_to_tree(struct file *dir_file,
  514. struct inode *dir, ext4_lblk_t block,
  515. struct dx_hash_info *hinfo,
  516. __u32 start_hash, __u32 start_minor_hash)
  517. {
  518. struct buffer_head *bh;
  519. struct ext4_dir_entry_2 *de, *top;
  520. int err, count = 0;
  521. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  522. (unsigned long)block));
  523. if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
  524. return err;
  525. de = (struct ext4_dir_entry_2 *) bh->b_data;
  526. top = (struct ext4_dir_entry_2 *) ((char *) de +
  527. dir->i_sb->s_blocksize -
  528. EXT4_DIR_REC_LEN(0));
  529. for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
  530. if (ext4_check_dir_entry(dir, NULL, de, bh,
  531. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  532. + ((char *)de - bh->b_data))) {
  533. /* On error, skip the f_pos to the next block. */
  534. dir_file->f_pos = (dir_file->f_pos |
  535. (dir->i_sb->s_blocksize - 1)) + 1;
  536. brelse(bh);
  537. return count;
  538. }
  539. ext4fs_dirhash(de->name, de->name_len, hinfo);
  540. if ((hinfo->hash < start_hash) ||
  541. ((hinfo->hash == start_hash) &&
  542. (hinfo->minor_hash < start_minor_hash)))
  543. continue;
  544. if (de->inode == 0)
  545. continue;
  546. if ((err = ext4_htree_store_dirent(dir_file,
  547. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  548. brelse(bh);
  549. return err;
  550. }
  551. count++;
  552. }
  553. brelse(bh);
  554. return count;
  555. }
  556. /*
  557. * This function fills a red-black tree with information from a
  558. * directory. We start scanning the directory in hash order, starting
  559. * at start_hash and start_minor_hash.
  560. *
  561. * This function returns the number of entries inserted into the tree,
  562. * or a negative error code.
  563. */
  564. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  565. __u32 start_minor_hash, __u32 *next_hash)
  566. {
  567. struct dx_hash_info hinfo;
  568. struct ext4_dir_entry_2 *de;
  569. struct dx_frame frames[2], *frame;
  570. struct inode *dir;
  571. ext4_lblk_t block;
  572. int count = 0;
  573. int ret, err;
  574. __u32 hashval;
  575. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  576. start_hash, start_minor_hash));
  577. dir = dir_file->f_path.dentry->d_inode;
  578. if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
  579. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  580. if (hinfo.hash_version <= DX_HASH_TEA)
  581. hinfo.hash_version +=
  582. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  583. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  584. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  585. start_hash, start_minor_hash);
  586. *next_hash = ~0;
  587. return count;
  588. }
  589. hinfo.hash = start_hash;
  590. hinfo.minor_hash = 0;
  591. frame = dx_probe(NULL, dir, &hinfo, frames, &err);
  592. if (!frame)
  593. return err;
  594. /* Add '.' and '..' from the htree header */
  595. if (!start_hash && !start_minor_hash) {
  596. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  597. if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  598. goto errout;
  599. count++;
  600. }
  601. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  602. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  603. de = ext4_next_entry(de, dir->i_sb->s_blocksize);
  604. if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  605. goto errout;
  606. count++;
  607. }
  608. while (1) {
  609. block = dx_get_block(frame->at);
  610. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  611. start_hash, start_minor_hash);
  612. if (ret < 0) {
  613. err = ret;
  614. goto errout;
  615. }
  616. count += ret;
  617. hashval = ~0;
  618. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  619. frame, frames, &hashval);
  620. *next_hash = hashval;
  621. if (ret < 0) {
  622. err = ret;
  623. goto errout;
  624. }
  625. /*
  626. * Stop if: (a) there are no more entries, or
  627. * (b) we have inserted at least one entry and the
  628. * next hash value is not a continuation
  629. */
  630. if ((ret == 0) ||
  631. (count && ((hashval & 1) == 0)))
  632. break;
  633. }
  634. dx_release(frames);
  635. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  636. "next hash: %x\n", count, *next_hash));
  637. return count;
  638. errout:
  639. dx_release(frames);
  640. return (err);
  641. }
  642. /*
  643. * Directory block splitting, compacting
  644. */
  645. /*
  646. * Create map of hash values, offsets, and sizes, stored at end of block.
  647. * Returns number of entries mapped.
  648. */
  649. static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
  650. struct dx_hash_info *hinfo,
  651. struct dx_map_entry *map_tail)
  652. {
  653. int count = 0;
  654. char *base = (char *) de;
  655. struct dx_hash_info h = *hinfo;
  656. while ((char *) de < base + blocksize) {
  657. if (de->name_len && de->inode) {
  658. ext4fs_dirhash(de->name, de->name_len, &h);
  659. map_tail--;
  660. map_tail->hash = h.hash;
  661. map_tail->offs = ((char *) de - base)>>2;
  662. map_tail->size = le16_to_cpu(de->rec_len);
  663. count++;
  664. cond_resched();
  665. }
  666. /* XXX: do we need to check rec_len == 0 case? -Chris */
  667. de = ext4_next_entry(de, blocksize);
  668. }
  669. return count;
  670. }
  671. /* Sort map by hash value */
  672. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  673. {
  674. struct dx_map_entry *p, *q, *top = map + count - 1;
  675. int more;
  676. /* Combsort until bubble sort doesn't suck */
  677. while (count > 2) {
  678. count = count*10/13;
  679. if (count - 9 < 2) /* 9, 10 -> 11 */
  680. count = 11;
  681. for (p = top, q = p - count; q >= map; p--, q--)
  682. if (p->hash < q->hash)
  683. swap(*p, *q);
  684. }
  685. /* Garden variety bubble sort */
  686. do {
  687. more = 0;
  688. q = top;
  689. while (q-- > map) {
  690. if (q[1].hash >= q[0].hash)
  691. continue;
  692. swap(*(q+1), *q);
  693. more = 1;
  694. }
  695. } while(more);
  696. }
  697. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  698. {
  699. struct dx_entry *entries = frame->entries;
  700. struct dx_entry *old = frame->at, *new = old + 1;
  701. int count = dx_get_count(entries);
  702. assert(count < dx_get_limit(entries));
  703. assert(old < entries + count);
  704. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  705. dx_set_hash(new, hash);
  706. dx_set_block(new, block);
  707. dx_set_count(entries, count + 1);
  708. }
  709. static void ext4_update_dx_flag(struct inode *inode)
  710. {
  711. if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  712. EXT4_FEATURE_COMPAT_DIR_INDEX))
  713. ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
  714. }
  715. /*
  716. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  717. *
  718. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  719. * `de != NULL' is guaranteed by caller.
  720. */
  721. static inline int ext4_match (int len, const char * const name,
  722. struct ext4_dir_entry_2 * de)
  723. {
  724. if (len != de->name_len)
  725. return 0;
  726. if (!de->inode)
  727. return 0;
  728. return !memcmp(name, de->name, len);
  729. }
  730. /*
  731. * Returns 0 if not found, -1 on failure, and 1 on success
  732. */
  733. static inline int search_dirblock(struct buffer_head *bh,
  734. struct inode *dir,
  735. const struct qstr *d_name,
  736. unsigned int offset,
  737. struct ext4_dir_entry_2 ** res_dir)
  738. {
  739. struct ext4_dir_entry_2 * de;
  740. char * dlimit;
  741. int de_len;
  742. const char *name = d_name->name;
  743. int namelen = d_name->len;
  744. de = (struct ext4_dir_entry_2 *) bh->b_data;
  745. dlimit = bh->b_data + dir->i_sb->s_blocksize;
  746. while ((char *) de < dlimit) {
  747. /* this code is executed quadratically often */
  748. /* do minimal checking `by hand' */
  749. if ((char *) de + namelen <= dlimit &&
  750. ext4_match (namelen, name, de)) {
  751. /* found a match - just to be sure, do a full check */
  752. if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
  753. return -1;
  754. *res_dir = de;
  755. return 1;
  756. }
  757. /* prevent looping on a bad block */
  758. de_len = ext4_rec_len_from_disk(de->rec_len,
  759. dir->i_sb->s_blocksize);
  760. if (de_len <= 0)
  761. return -1;
  762. offset += de_len;
  763. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  764. }
  765. return 0;
  766. }
  767. /*
  768. * ext4_find_entry()
  769. *
  770. * finds an entry in the specified directory with the wanted name. It
  771. * returns the cache buffer in which the entry was found, and the entry
  772. * itself (as a parameter - res_dir). It does NOT read the inode of the
  773. * entry - you'll have to do that yourself if you want to.
  774. *
  775. * The returned buffer_head has ->b_count elevated. The caller is expected
  776. * to brelse() it when appropriate.
  777. */
  778. static struct buffer_head * ext4_find_entry (struct inode *dir,
  779. const struct qstr *d_name,
  780. struct ext4_dir_entry_2 ** res_dir)
  781. {
  782. struct super_block *sb;
  783. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  784. struct buffer_head *bh, *ret = NULL;
  785. ext4_lblk_t start, block, b;
  786. const u8 *name = d_name->name;
  787. int ra_max = 0; /* Number of bh's in the readahead
  788. buffer, bh_use[] */
  789. int ra_ptr = 0; /* Current index into readahead
  790. buffer */
  791. int num = 0;
  792. ext4_lblk_t nblocks;
  793. int i, err;
  794. int namelen;
  795. *res_dir = NULL;
  796. sb = dir->i_sb;
  797. namelen = d_name->len;
  798. if (namelen > EXT4_NAME_LEN)
  799. return NULL;
  800. if ((namelen <= 2) && (name[0] == '.') &&
  801. (name[1] == '.' || name[1] == '\0')) {
  802. /*
  803. * "." or ".." will only be in the first block
  804. * NFS may look up ".."; "." should be handled by the VFS
  805. */
  806. block = start = 0;
  807. nblocks = 1;
  808. goto restart;
  809. }
  810. if (is_dx(dir)) {
  811. bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
  812. /*
  813. * On success, or if the error was file not found,
  814. * return. Otherwise, fall back to doing a search the
  815. * old fashioned way.
  816. */
  817. if (bh || (err != ERR_BAD_DX_DIR))
  818. return bh;
  819. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  820. "falling back\n"));
  821. }
  822. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  823. start = EXT4_I(dir)->i_dir_start_lookup;
  824. if (start >= nblocks)
  825. start = 0;
  826. block = start;
  827. restart:
  828. do {
  829. /*
  830. * We deal with the read-ahead logic here.
  831. */
  832. if (ra_ptr >= ra_max) {
  833. /* Refill the readahead buffer */
  834. ra_ptr = 0;
  835. b = block;
  836. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  837. /*
  838. * Terminate if we reach the end of the
  839. * directory and must wrap, or if our
  840. * search has finished at this block.
  841. */
  842. if (b >= nblocks || (num && block == start)) {
  843. bh_use[ra_max] = NULL;
  844. break;
  845. }
  846. num++;
  847. bh = ext4_getblk(NULL, dir, b++, 0, &err);
  848. bh_use[ra_max] = bh;
  849. if (bh)
  850. ll_rw_block(READ_META, 1, &bh);
  851. }
  852. }
  853. if ((bh = bh_use[ra_ptr++]) == NULL)
  854. goto next;
  855. wait_on_buffer(bh);
  856. if (!buffer_uptodate(bh)) {
  857. /* read error, skip block & hope for the best */
  858. EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
  859. (unsigned long) block);
  860. brelse(bh);
  861. goto next;
  862. }
  863. i = search_dirblock(bh, dir, d_name,
  864. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  865. if (i == 1) {
  866. EXT4_I(dir)->i_dir_start_lookup = block;
  867. ret = bh;
  868. goto cleanup_and_exit;
  869. } else {
  870. brelse(bh);
  871. if (i < 0)
  872. goto cleanup_and_exit;
  873. }
  874. next:
  875. if (++block >= nblocks)
  876. block = 0;
  877. } while (block != start);
  878. /*
  879. * If the directory has grown while we were searching, then
  880. * search the last part of the directory before giving up.
  881. */
  882. block = nblocks;
  883. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  884. if (block < nblocks) {
  885. start = 0;
  886. goto restart;
  887. }
  888. cleanup_and_exit:
  889. /* Clean up the read-ahead blocks */
  890. for (; ra_ptr < ra_max; ra_ptr++)
  891. brelse(bh_use[ra_ptr]);
  892. return ret;
  893. }
  894. static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
  895. struct ext4_dir_entry_2 **res_dir, int *err)
  896. {
  897. struct super_block * sb = dir->i_sb;
  898. struct dx_hash_info hinfo;
  899. struct dx_frame frames[2], *frame;
  900. struct buffer_head *bh;
  901. ext4_lblk_t block;
  902. int retval;
  903. if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
  904. return NULL;
  905. do {
  906. block = dx_get_block(frame->at);
  907. if (!(bh = ext4_bread(NULL, dir, block, 0, err)))
  908. goto errout;
  909. retval = search_dirblock(bh, dir, d_name,
  910. block << EXT4_BLOCK_SIZE_BITS(sb),
  911. res_dir);
  912. if (retval == 1) { /* Success! */
  913. dx_release(frames);
  914. return bh;
  915. }
  916. brelse(bh);
  917. if (retval == -1) {
  918. *err = ERR_BAD_DX_DIR;
  919. goto errout;
  920. }
  921. /* Check to see if we should continue to search */
  922. retval = ext4_htree_next_block(dir, hinfo.hash, frame,
  923. frames, NULL);
  924. if (retval < 0) {
  925. ext4_warning(sb,
  926. "error reading index page in directory #%lu",
  927. dir->i_ino);
  928. *err = retval;
  929. goto errout;
  930. }
  931. } while (retval == 1);
  932. *err = -ENOENT;
  933. errout:
  934. dxtrace(printk(KERN_DEBUG "%s not found\n", name));
  935. dx_release (frames);
  936. return NULL;
  937. }
  938. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  939. {
  940. struct inode *inode;
  941. struct ext4_dir_entry_2 *de;
  942. struct buffer_head *bh;
  943. if (dentry->d_name.len > EXT4_NAME_LEN)
  944. return ERR_PTR(-ENAMETOOLONG);
  945. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  946. inode = NULL;
  947. if (bh) {
  948. __u32 ino = le32_to_cpu(de->inode);
  949. brelse(bh);
  950. if (!ext4_valid_inum(dir->i_sb, ino)) {
  951. EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
  952. return ERR_PTR(-EIO);
  953. }
  954. inode = ext4_iget(dir->i_sb, ino);
  955. if (IS_ERR(inode)) {
  956. if (PTR_ERR(inode) == -ESTALE) {
  957. EXT4_ERROR_INODE(dir,
  958. "deleted inode referenced: %u",
  959. ino);
  960. return ERR_PTR(-EIO);
  961. } else {
  962. return ERR_CAST(inode);
  963. }
  964. }
  965. }
  966. return d_splice_alias(inode, dentry);
  967. }
  968. struct dentry *ext4_get_parent(struct dentry *child)
  969. {
  970. __u32 ino;
  971. static const struct qstr dotdot = {
  972. .name = "..",
  973. .len = 2,
  974. };
  975. struct ext4_dir_entry_2 * de;
  976. struct buffer_head *bh;
  977. bh = ext4_find_entry(child->d_inode, &dotdot, &de);
  978. if (!bh)
  979. return ERR_PTR(-ENOENT);
  980. ino = le32_to_cpu(de->inode);
  981. brelse(bh);
  982. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  983. EXT4_ERROR_INODE(child->d_inode,
  984. "bad parent inode number: %u", ino);
  985. return ERR_PTR(-EIO);
  986. }
  987. return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
  988. }
  989. #define S_SHIFT 12
  990. static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
  991. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  992. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  993. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  994. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  995. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  996. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  997. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  998. };
  999. static inline void ext4_set_de_type(struct super_block *sb,
  1000. struct ext4_dir_entry_2 *de,
  1001. umode_t mode) {
  1002. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
  1003. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  1004. }
  1005. /*
  1006. * Move count entries from end of map between two memory locations.
  1007. * Returns pointer to last entry moved.
  1008. */
  1009. static struct ext4_dir_entry_2 *
  1010. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
  1011. unsigned blocksize)
  1012. {
  1013. unsigned rec_len = 0;
  1014. while (count--) {
  1015. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1016. (from + (map->offs<<2));
  1017. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1018. memcpy (to, de, rec_len);
  1019. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1020. ext4_rec_len_to_disk(rec_len, blocksize);
  1021. de->inode = 0;
  1022. map++;
  1023. to += rec_len;
  1024. }
  1025. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1026. }
  1027. /*
  1028. * Compact each dir entry in the range to the minimal rec_len.
  1029. * Returns pointer to last entry in range.
  1030. */
  1031. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
  1032. {
  1033. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1034. unsigned rec_len = 0;
  1035. prev = to = de;
  1036. while ((char*)de < base + blocksize) {
  1037. next = ext4_next_entry(de, blocksize);
  1038. if (de->inode && de->name_len) {
  1039. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1040. if (de > to)
  1041. memmove(to, de, rec_len);
  1042. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1043. prev = to;
  1044. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1045. }
  1046. de = next;
  1047. }
  1048. return prev;
  1049. }
  1050. /*
  1051. * Split a full leaf block to make room for a new dir entry.
  1052. * Allocate a new block, and move entries so that they are approx. equally full.
  1053. * Returns pointer to de in block into which the new entry will be inserted.
  1054. */
  1055. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1056. struct buffer_head **bh,struct dx_frame *frame,
  1057. struct dx_hash_info *hinfo, int *error)
  1058. {
  1059. unsigned blocksize = dir->i_sb->s_blocksize;
  1060. unsigned count, continued;
  1061. struct buffer_head *bh2;
  1062. ext4_lblk_t newblock;
  1063. u32 hash2;
  1064. struct dx_map_entry *map;
  1065. char *data1 = (*bh)->b_data, *data2;
  1066. unsigned split, move, size;
  1067. struct ext4_dir_entry_2 *de = NULL, *de2;
  1068. int err = 0, i;
  1069. bh2 = ext4_append (handle, dir, &newblock, &err);
  1070. if (!(bh2)) {
  1071. brelse(*bh);
  1072. *bh = NULL;
  1073. goto errout;
  1074. }
  1075. BUFFER_TRACE(*bh, "get_write_access");
  1076. err = ext4_journal_get_write_access(handle, *bh);
  1077. if (err)
  1078. goto journal_error;
  1079. BUFFER_TRACE(frame->bh, "get_write_access");
  1080. err = ext4_journal_get_write_access(handle, frame->bh);
  1081. if (err)
  1082. goto journal_error;
  1083. data2 = bh2->b_data;
  1084. /* create map in the end of data2 block */
  1085. map = (struct dx_map_entry *) (data2 + blocksize);
  1086. count = dx_make_map((struct ext4_dir_entry_2 *) data1,
  1087. blocksize, hinfo, map);
  1088. map -= count;
  1089. dx_sort_map(map, count);
  1090. /* Split the existing block in the middle, size-wise */
  1091. size = 0;
  1092. move = 0;
  1093. for (i = count-1; i >= 0; i--) {
  1094. /* is more than half of this entry in 2nd half of the block? */
  1095. if (size + map[i].size/2 > blocksize/2)
  1096. break;
  1097. size += map[i].size;
  1098. move++;
  1099. }
  1100. /* map index at which we will split */
  1101. split = count - move;
  1102. hash2 = map[split].hash;
  1103. continued = hash2 == map[split - 1].hash;
  1104. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1105. (unsigned long)dx_get_block(frame->at),
  1106. hash2, split, count-split));
  1107. /* Fancy dance to stay within two buffers */
  1108. de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
  1109. de = dx_pack_dirents(data1, blocksize);
  1110. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
  1111. blocksize);
  1112. de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2,
  1113. blocksize);
  1114. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1115. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1116. /* Which block gets the new entry? */
  1117. if (hinfo->hash >= hash2)
  1118. {
  1119. swap(*bh, bh2);
  1120. de = de2;
  1121. }
  1122. dx_insert_block(frame, hash2 + continued, newblock);
  1123. err = ext4_handle_dirty_metadata(handle, dir, bh2);
  1124. if (err)
  1125. goto journal_error;
  1126. err = ext4_handle_dirty_metadata(handle, dir, frame->bh);
  1127. if (err)
  1128. goto journal_error;
  1129. brelse(bh2);
  1130. dxtrace(dx_show_index("frame", frame->entries));
  1131. return de;
  1132. journal_error:
  1133. brelse(*bh);
  1134. brelse(bh2);
  1135. *bh = NULL;
  1136. ext4_std_error(dir->i_sb, err);
  1137. errout:
  1138. *error = err;
  1139. return NULL;
  1140. }
  1141. /*
  1142. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1143. * it points to a directory entry which is guaranteed to be large
  1144. * enough for new directory entry. If de is NULL, then
  1145. * add_dirent_to_buf will attempt search the directory block for
  1146. * space. It will return -ENOSPC if no space is available, and -EIO
  1147. * and -EEXIST if directory entry already exists.
  1148. */
  1149. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1150. struct inode *inode, struct ext4_dir_entry_2 *de,
  1151. struct buffer_head *bh)
  1152. {
  1153. struct inode *dir = dentry->d_parent->d_inode;
  1154. const char *name = dentry->d_name.name;
  1155. int namelen = dentry->d_name.len;
  1156. unsigned int offset = 0;
  1157. unsigned int blocksize = dir->i_sb->s_blocksize;
  1158. unsigned short reclen;
  1159. int nlen, rlen, err;
  1160. char *top;
  1161. reclen = EXT4_DIR_REC_LEN(namelen);
  1162. if (!de) {
  1163. de = (struct ext4_dir_entry_2 *)bh->b_data;
  1164. top = bh->b_data + blocksize - reclen;
  1165. while ((char *) de <= top) {
  1166. if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
  1167. return -EIO;
  1168. if (ext4_match(namelen, name, de))
  1169. return -EEXIST;
  1170. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1171. rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
  1172. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1173. break;
  1174. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1175. offset += rlen;
  1176. }
  1177. if ((char *) de > top)
  1178. return -ENOSPC;
  1179. }
  1180. BUFFER_TRACE(bh, "get_write_access");
  1181. err = ext4_journal_get_write_access(handle, bh);
  1182. if (err) {
  1183. ext4_std_error(dir->i_sb, err);
  1184. return err;
  1185. }
  1186. /* By now the buffer is marked for journaling */
  1187. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1188. rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
  1189. if (de->inode) {
  1190. struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1191. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, blocksize);
  1192. de->rec_len = ext4_rec_len_to_disk(nlen, blocksize);
  1193. de = de1;
  1194. }
  1195. de->file_type = EXT4_FT_UNKNOWN;
  1196. if (inode) {
  1197. de->inode = cpu_to_le32(inode->i_ino);
  1198. ext4_set_de_type(dir->i_sb, de, inode->i_mode);
  1199. } else
  1200. de->inode = 0;
  1201. de->name_len = namelen;
  1202. memcpy(de->name, name, namelen);
  1203. /*
  1204. * XXX shouldn't update any times until successful
  1205. * completion of syscall, but too many callers depend
  1206. * on this.
  1207. *
  1208. * XXX similarly, too many callers depend on
  1209. * ext4_new_inode() setting the times, but error
  1210. * recovery deletes the inode, so the worst that can
  1211. * happen is that the times are slightly out of date
  1212. * and/or different from the directory change time.
  1213. */
  1214. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1215. ext4_update_dx_flag(dir);
  1216. dir->i_version++;
  1217. ext4_mark_inode_dirty(handle, dir);
  1218. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1219. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1220. if (err)
  1221. ext4_std_error(dir->i_sb, err);
  1222. return 0;
  1223. }
  1224. /*
  1225. * This converts a one block unindexed directory to a 3 block indexed
  1226. * directory, and adds the dentry to the indexed directory.
  1227. */
  1228. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1229. struct inode *inode, struct buffer_head *bh)
  1230. {
  1231. struct inode *dir = dentry->d_parent->d_inode;
  1232. const char *name = dentry->d_name.name;
  1233. int namelen = dentry->d_name.len;
  1234. struct buffer_head *bh2;
  1235. struct dx_root *root;
  1236. struct dx_frame frames[2], *frame;
  1237. struct dx_entry *entries;
  1238. struct ext4_dir_entry_2 *de, *de2;
  1239. char *data1, *top;
  1240. unsigned len;
  1241. int retval;
  1242. unsigned blocksize;
  1243. struct dx_hash_info hinfo;
  1244. ext4_lblk_t block;
  1245. struct fake_dirent *fde;
  1246. blocksize = dir->i_sb->s_blocksize;
  1247. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1248. retval = ext4_journal_get_write_access(handle, bh);
  1249. if (retval) {
  1250. ext4_std_error(dir->i_sb, retval);
  1251. brelse(bh);
  1252. return retval;
  1253. }
  1254. root = (struct dx_root *) bh->b_data;
  1255. /* The 0th block becomes the root, move the dirents out */
  1256. fde = &root->dotdot;
  1257. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1258. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  1259. if ((char *) de >= (((char *) root) + blocksize)) {
  1260. EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
  1261. brelse(bh);
  1262. return -EIO;
  1263. }
  1264. len = ((char *) root) + blocksize - (char *) de;
  1265. /* Allocate new block for the 0th block's dirents */
  1266. bh2 = ext4_append(handle, dir, &block, &retval);
  1267. if (!(bh2)) {
  1268. brelse(bh);
  1269. return retval;
  1270. }
  1271. ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
  1272. data1 = bh2->b_data;
  1273. memcpy (data1, de, len);
  1274. de = (struct ext4_dir_entry_2 *) data1;
  1275. top = data1 + len;
  1276. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
  1277. de = de2;
  1278. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
  1279. blocksize);
  1280. /* Initialize the root; the dot dirents already exist */
  1281. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1282. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
  1283. blocksize);
  1284. memset (&root->info, 0, sizeof(root->info));
  1285. root->info.info_length = sizeof(root->info);
  1286. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1287. entries = root->entries;
  1288. dx_set_block(entries, 1);
  1289. dx_set_count(entries, 1);
  1290. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1291. /* Initialize as for dx_probe */
  1292. hinfo.hash_version = root->info.hash_version;
  1293. if (hinfo.hash_version <= DX_HASH_TEA)
  1294. hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1295. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1296. ext4fs_dirhash(name, namelen, &hinfo);
  1297. frame = frames;
  1298. frame->entries = entries;
  1299. frame->at = entries;
  1300. frame->bh = bh;
  1301. bh = bh2;
  1302. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1303. dx_release (frames);
  1304. if (!(de))
  1305. return retval;
  1306. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1307. brelse(bh);
  1308. return retval;
  1309. }
  1310. /*
  1311. * ext4_add_entry()
  1312. *
  1313. * adds a file entry to the specified directory, using the same
  1314. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1315. *
  1316. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1317. * may not sleep between calling this and putting something into
  1318. * the entry, as someone else might have used it while you slept.
  1319. */
  1320. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1321. struct inode *inode)
  1322. {
  1323. struct inode *dir = dentry->d_parent->d_inode;
  1324. struct buffer_head *bh;
  1325. struct ext4_dir_entry_2 *de;
  1326. struct super_block *sb;
  1327. int retval;
  1328. int dx_fallback=0;
  1329. unsigned blocksize;
  1330. ext4_lblk_t block, blocks;
  1331. sb = dir->i_sb;
  1332. blocksize = sb->s_blocksize;
  1333. if (!dentry->d_name.len)
  1334. return -EINVAL;
  1335. if (is_dx(dir)) {
  1336. retval = ext4_dx_add_entry(handle, dentry, inode);
  1337. if (!retval || (retval != ERR_BAD_DX_DIR))
  1338. return retval;
  1339. ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
  1340. dx_fallback++;
  1341. ext4_mark_inode_dirty(handle, dir);
  1342. }
  1343. blocks = dir->i_size >> sb->s_blocksize_bits;
  1344. for (block = 0; block < blocks; block++) {
  1345. bh = ext4_bread(handle, dir, block, 0, &retval);
  1346. if(!bh)
  1347. return retval;
  1348. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1349. if (retval != -ENOSPC) {
  1350. brelse(bh);
  1351. return retval;
  1352. }
  1353. if (blocks == 1 && !dx_fallback &&
  1354. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1355. return make_indexed_dir(handle, dentry, inode, bh);
  1356. brelse(bh);
  1357. }
  1358. bh = ext4_append(handle, dir, &block, &retval);
  1359. if (!bh)
  1360. return retval;
  1361. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1362. de->inode = 0;
  1363. de->rec_len = ext4_rec_len_to_disk(blocksize, blocksize);
  1364. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1365. brelse(bh);
  1366. if (retval == 0)
  1367. ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
  1368. return retval;
  1369. }
  1370. /*
  1371. * Returns 0 for success, or a negative error value
  1372. */
  1373. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1374. struct inode *inode)
  1375. {
  1376. struct dx_frame frames[2], *frame;
  1377. struct dx_entry *entries, *at;
  1378. struct dx_hash_info hinfo;
  1379. struct buffer_head *bh;
  1380. struct inode *dir = dentry->d_parent->d_inode;
  1381. struct super_block *sb = dir->i_sb;
  1382. struct ext4_dir_entry_2 *de;
  1383. int err;
  1384. frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
  1385. if (!frame)
  1386. return err;
  1387. entries = frame->entries;
  1388. at = frame->at;
  1389. if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1390. goto cleanup;
  1391. BUFFER_TRACE(bh, "get_write_access");
  1392. err = ext4_journal_get_write_access(handle, bh);
  1393. if (err)
  1394. goto journal_error;
  1395. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1396. if (err != -ENOSPC)
  1397. goto cleanup;
  1398. /* Block full, should compress but for now just split */
  1399. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1400. dx_get_count(entries), dx_get_limit(entries)));
  1401. /* Need to split index? */
  1402. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1403. ext4_lblk_t newblock;
  1404. unsigned icount = dx_get_count(entries);
  1405. int levels = frame - frames;
  1406. struct dx_entry *entries2;
  1407. struct dx_node *node2;
  1408. struct buffer_head *bh2;
  1409. if (levels && (dx_get_count(frames->entries) ==
  1410. dx_get_limit(frames->entries))) {
  1411. ext4_warning(sb, "Directory index full!");
  1412. err = -ENOSPC;
  1413. goto cleanup;
  1414. }
  1415. bh2 = ext4_append (handle, dir, &newblock, &err);
  1416. if (!(bh2))
  1417. goto cleanup;
  1418. node2 = (struct dx_node *)(bh2->b_data);
  1419. entries2 = node2->entries;
  1420. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  1421. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  1422. sb->s_blocksize);
  1423. BUFFER_TRACE(frame->bh, "get_write_access");
  1424. err = ext4_journal_get_write_access(handle, frame->bh);
  1425. if (err)
  1426. goto journal_error;
  1427. if (levels) {
  1428. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1429. unsigned hash2 = dx_get_hash(entries + icount1);
  1430. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  1431. icount1, icount2));
  1432. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1433. err = ext4_journal_get_write_access(handle,
  1434. frames[0].bh);
  1435. if (err)
  1436. goto journal_error;
  1437. memcpy((char *) entries2, (char *) (entries + icount1),
  1438. icount2 * sizeof(struct dx_entry));
  1439. dx_set_count(entries, icount1);
  1440. dx_set_count(entries2, icount2);
  1441. dx_set_limit(entries2, dx_node_limit(dir));
  1442. /* Which index block gets the new entry? */
  1443. if (at - entries >= icount1) {
  1444. frame->at = at = at - entries - icount1 + entries2;
  1445. frame->entries = entries = entries2;
  1446. swap(frame->bh, bh2);
  1447. }
  1448. dx_insert_block(frames + 0, hash2, newblock);
  1449. dxtrace(dx_show_index("node", frames[1].entries));
  1450. dxtrace(dx_show_index("node",
  1451. ((struct dx_node *) bh2->b_data)->entries));
  1452. err = ext4_handle_dirty_metadata(handle, inode, bh2);
  1453. if (err)
  1454. goto journal_error;
  1455. brelse (bh2);
  1456. } else {
  1457. dxtrace(printk(KERN_DEBUG
  1458. "Creating second level index...\n"));
  1459. memcpy((char *) entries2, (char *) entries,
  1460. icount * sizeof(struct dx_entry));
  1461. dx_set_limit(entries2, dx_node_limit(dir));
  1462. /* Set up root */
  1463. dx_set_count(entries, 1);
  1464. dx_set_block(entries + 0, newblock);
  1465. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1466. /* Add new access path frame */
  1467. frame = frames + 1;
  1468. frame->at = at = at - entries + entries2;
  1469. frame->entries = entries = entries2;
  1470. frame->bh = bh2;
  1471. err = ext4_journal_get_write_access(handle,
  1472. frame->bh);
  1473. if (err)
  1474. goto journal_error;
  1475. }
  1476. err = ext4_handle_dirty_metadata(handle, inode, frames[0].bh);
  1477. if (err) {
  1478. ext4_std_error(inode->i_sb, err);
  1479. goto cleanup;
  1480. }
  1481. }
  1482. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1483. if (!de)
  1484. goto cleanup;
  1485. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1486. goto cleanup;
  1487. journal_error:
  1488. ext4_std_error(dir->i_sb, err);
  1489. cleanup:
  1490. if (bh)
  1491. brelse(bh);
  1492. dx_release(frames);
  1493. return err;
  1494. }
  1495. /*
  1496. * ext4_delete_entry deletes a directory entry by merging it with the
  1497. * previous entry
  1498. */
  1499. static int ext4_delete_entry(handle_t *handle,
  1500. struct inode *dir,
  1501. struct ext4_dir_entry_2 *de_del,
  1502. struct buffer_head *bh)
  1503. {
  1504. struct ext4_dir_entry_2 *de, *pde;
  1505. unsigned int blocksize = dir->i_sb->s_blocksize;
  1506. int i, err;
  1507. i = 0;
  1508. pde = NULL;
  1509. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1510. while (i < bh->b_size) {
  1511. if (ext4_check_dir_entry(dir, NULL, de, bh, i))
  1512. return -EIO;
  1513. if (de == de_del) {
  1514. BUFFER_TRACE(bh, "get_write_access");
  1515. err = ext4_journal_get_write_access(handle, bh);
  1516. if (unlikely(err)) {
  1517. ext4_std_error(dir->i_sb, err);
  1518. return err;
  1519. }
  1520. if (pde)
  1521. pde->rec_len = ext4_rec_len_to_disk(
  1522. ext4_rec_len_from_disk(pde->rec_len,
  1523. blocksize) +
  1524. ext4_rec_len_from_disk(de->rec_len,
  1525. blocksize),
  1526. blocksize);
  1527. else
  1528. de->inode = 0;
  1529. dir->i_version++;
  1530. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1531. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1532. if (unlikely(err)) {
  1533. ext4_std_error(dir->i_sb, err);
  1534. return err;
  1535. }
  1536. return 0;
  1537. }
  1538. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  1539. pde = de;
  1540. de = ext4_next_entry(de, blocksize);
  1541. }
  1542. return -ENOENT;
  1543. }
  1544. /*
  1545. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  1546. * since this indicates that nlinks count was previously 1.
  1547. */
  1548. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  1549. {
  1550. inc_nlink(inode);
  1551. if (is_dx(inode) && inode->i_nlink > 1) {
  1552. /* limit is 16-bit i_links_count */
  1553. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  1554. inode->i_nlink = 1;
  1555. EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
  1556. EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
  1557. }
  1558. }
  1559. }
  1560. /*
  1561. * If a directory had nlink == 1, then we should let it be 1. This indicates
  1562. * directory has >EXT4_LINK_MAX subdirs.
  1563. */
  1564. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  1565. {
  1566. drop_nlink(inode);
  1567. if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
  1568. inc_nlink(inode);
  1569. }
  1570. static int ext4_add_nondir(handle_t *handle,
  1571. struct dentry *dentry, struct inode *inode)
  1572. {
  1573. int err = ext4_add_entry(handle, dentry, inode);
  1574. if (!err) {
  1575. ext4_mark_inode_dirty(handle, inode);
  1576. d_instantiate(dentry, inode);
  1577. unlock_new_inode(inode);
  1578. return 0;
  1579. }
  1580. drop_nlink(inode);
  1581. unlock_new_inode(inode);
  1582. iput(inode);
  1583. return err;
  1584. }
  1585. /*
  1586. * By the time this is called, we already have created
  1587. * the directory cache entry for the new file, but it
  1588. * is so far negative - it has no inode.
  1589. *
  1590. * If the create succeeds, we fill in the inode information
  1591. * with d_instantiate().
  1592. */
  1593. static int ext4_create(struct inode *dir, struct dentry *dentry, int mode,
  1594. struct nameidata *nd)
  1595. {
  1596. handle_t *handle;
  1597. struct inode *inode;
  1598. int err, retries = 0;
  1599. dquot_initialize(dir);
  1600. retry:
  1601. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1602. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1603. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1604. if (IS_ERR(handle))
  1605. return PTR_ERR(handle);
  1606. if (IS_DIRSYNC(dir))
  1607. ext4_handle_sync(handle);
  1608. inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0);
  1609. err = PTR_ERR(inode);
  1610. if (!IS_ERR(inode)) {
  1611. inode->i_op = &ext4_file_inode_operations;
  1612. inode->i_fop = &ext4_file_operations;
  1613. ext4_set_aops(inode);
  1614. err = ext4_add_nondir(handle, dentry, inode);
  1615. }
  1616. ext4_journal_stop(handle);
  1617. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1618. goto retry;
  1619. return err;
  1620. }
  1621. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  1622. int mode, dev_t rdev)
  1623. {
  1624. handle_t *handle;
  1625. struct inode *inode;
  1626. int err, retries = 0;
  1627. if (!new_valid_dev(rdev))
  1628. return -EINVAL;
  1629. dquot_initialize(dir);
  1630. retry:
  1631. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1632. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1633. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1634. if (IS_ERR(handle))
  1635. return PTR_ERR(handle);
  1636. if (IS_DIRSYNC(dir))
  1637. ext4_handle_sync(handle);
  1638. inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0);
  1639. err = PTR_ERR(inode);
  1640. if (!IS_ERR(inode)) {
  1641. init_special_inode(inode, inode->i_mode, rdev);
  1642. #ifdef CONFIG_EXT4_FS_XATTR
  1643. inode->i_op = &ext4_special_inode_operations;
  1644. #endif
  1645. err = ext4_add_nondir(handle, dentry, inode);
  1646. }
  1647. ext4_journal_stop(handle);
  1648. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1649. goto retry;
  1650. return err;
  1651. }
  1652. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1653. {
  1654. handle_t *handle;
  1655. struct inode *inode;
  1656. struct buffer_head *dir_block = NULL;
  1657. struct ext4_dir_entry_2 *de;
  1658. unsigned int blocksize = dir->i_sb->s_blocksize;
  1659. int err, retries = 0;
  1660. if (EXT4_DIR_LINK_MAX(dir))
  1661. return -EMLINK;
  1662. dquot_initialize(dir);
  1663. retry:
  1664. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1665. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1666. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1667. if (IS_ERR(handle))
  1668. return PTR_ERR(handle);
  1669. if (IS_DIRSYNC(dir))
  1670. ext4_handle_sync(handle);
  1671. inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
  1672. &dentry->d_name, 0);
  1673. err = PTR_ERR(inode);
  1674. if (IS_ERR(inode))
  1675. goto out_stop;
  1676. inode->i_op = &ext4_dir_inode_operations;
  1677. inode->i_fop = &ext4_dir_operations;
  1678. inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1679. dir_block = ext4_bread(handle, inode, 0, 1, &err);
  1680. if (!dir_block)
  1681. goto out_clear_inode;
  1682. BUFFER_TRACE(dir_block, "get_write_access");
  1683. err = ext4_journal_get_write_access(handle, dir_block);
  1684. if (err)
  1685. goto out_clear_inode;
  1686. de = (struct ext4_dir_entry_2 *) dir_block->b_data;
  1687. de->inode = cpu_to_le32(inode->i_ino);
  1688. de->name_len = 1;
  1689. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
  1690. blocksize);
  1691. strcpy(de->name, ".");
  1692. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1693. de = ext4_next_entry(de, blocksize);
  1694. de->inode = cpu_to_le32(dir->i_ino);
  1695. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(1),
  1696. blocksize);
  1697. de->name_len = 2;
  1698. strcpy(de->name, "..");
  1699. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1700. inode->i_nlink = 2;
  1701. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  1702. err = ext4_handle_dirty_metadata(handle, dir, dir_block);
  1703. if (err)
  1704. goto out_clear_inode;
  1705. err = ext4_mark_inode_dirty(handle, inode);
  1706. if (!err)
  1707. err = ext4_add_entry(handle, dentry, inode);
  1708. if (err) {
  1709. out_clear_inode:
  1710. clear_nlink(inode);
  1711. unlock_new_inode(inode);
  1712. ext4_mark_inode_dirty(handle, inode);
  1713. iput(inode);
  1714. goto out_stop;
  1715. }
  1716. ext4_inc_count(handle, dir);
  1717. ext4_update_dx_flag(dir);
  1718. err = ext4_mark_inode_dirty(handle, dir);
  1719. if (err)
  1720. goto out_clear_inode;
  1721. d_instantiate(dentry, inode);
  1722. unlock_new_inode(inode);
  1723. out_stop:
  1724. brelse(dir_block);
  1725. ext4_journal_stop(handle);
  1726. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1727. goto retry;
  1728. return err;
  1729. }
  1730. /*
  1731. * routine to check that the specified directory is empty (for rmdir)
  1732. */
  1733. static int empty_dir(struct inode *inode)
  1734. {
  1735. unsigned int offset;
  1736. struct buffer_head *bh;
  1737. struct ext4_dir_entry_2 *de, *de1;
  1738. struct super_block *sb;
  1739. int err = 0;
  1740. sb = inode->i_sb;
  1741. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
  1742. !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
  1743. if (err)
  1744. EXT4_ERROR_INODE(inode,
  1745. "error %d reading directory lblock 0", err);
  1746. else
  1747. ext4_warning(inode->i_sb,
  1748. "bad directory (dir #%lu) - no data block",
  1749. inode->i_ino);
  1750. return 1;
  1751. }
  1752. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1753. de1 = ext4_next_entry(de, sb->s_blocksize);
  1754. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1755. !le32_to_cpu(de1->inode) ||
  1756. strcmp(".", de->name) ||
  1757. strcmp("..", de1->name)) {
  1758. ext4_warning(inode->i_sb,
  1759. "bad directory (dir #%lu) - no `.' or `..'",
  1760. inode->i_ino);
  1761. brelse(bh);
  1762. return 1;
  1763. }
  1764. offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
  1765. ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
  1766. de = ext4_next_entry(de1, sb->s_blocksize);
  1767. while (offset < inode->i_size) {
  1768. if (!bh ||
  1769. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1770. unsigned int lblock;
  1771. err = 0;
  1772. brelse(bh);
  1773. lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  1774. bh = ext4_bread(NULL, inode, lblock, 0, &err);
  1775. if (!bh) {
  1776. if (err)
  1777. EXT4_ERROR_INODE(inode,
  1778. "error %d reading directory "
  1779. "lblock %u", err, lblock);
  1780. offset += sb->s_blocksize;
  1781. continue;
  1782. }
  1783. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1784. }
  1785. if (ext4_check_dir_entry(inode, NULL, de, bh, offset)) {
  1786. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  1787. sb->s_blocksize);
  1788. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1789. continue;
  1790. }
  1791. if (le32_to_cpu(de->inode)) {
  1792. brelse(bh);
  1793. return 0;
  1794. }
  1795. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  1796. de = ext4_next_entry(de, sb->s_blocksize);
  1797. }
  1798. brelse(bh);
  1799. return 1;
  1800. }
  1801. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  1802. * such inodes, starting at the superblock, in case we crash before the
  1803. * file is closed/deleted, or in case the inode truncate spans multiple
  1804. * transactions and the last transaction is not recovered after a crash.
  1805. *
  1806. * At filesystem recovery time, we walk this list deleting unlinked
  1807. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  1808. */
  1809. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  1810. {
  1811. struct super_block *sb = inode->i_sb;
  1812. struct ext4_iloc iloc;
  1813. int err = 0, rc;
  1814. if (!ext4_handle_valid(handle))
  1815. return 0;
  1816. mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
  1817. if (!list_empty(&EXT4_I(inode)->i_orphan))
  1818. goto out_unlock;
  1819. /* Orphan handling is only valid for files with data blocks
  1820. * being truncated, or files being unlinked. */
  1821. /* @@@ FIXME: Observation from aviro:
  1822. * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
  1823. * here (on s_orphan_lock), so race with ext4_link() which might bump
  1824. * ->i_nlink. For, say it, character device. Not a regular file,
  1825. * not a directory, not a symlink and ->i_nlink > 0.
  1826. *
  1827. * tytso, 4/25/2009: I'm not sure how that could happen;
  1828. * shouldn't the fs core protect us from these sort of
  1829. * unlink()/link() races?
  1830. */
  1831. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1832. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1833. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1834. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1835. if (err)
  1836. goto out_unlock;
  1837. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1838. if (err)
  1839. goto out_unlock;
  1840. /*
  1841. * Due to previous errors inode may be already a part of on-disk
  1842. * orphan list. If so skip on-disk list modification.
  1843. */
  1844. if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
  1845. (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
  1846. goto mem_insert;
  1847. /* Insert this inode at the head of the on-disk orphan list... */
  1848. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  1849. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1850. err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
  1851. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1852. if (!err)
  1853. err = rc;
  1854. /* Only add to the head of the in-memory list if all the
  1855. * previous operations succeeded. If the orphan_add is going to
  1856. * fail (possibly taking the journal offline), we can't risk
  1857. * leaving the inode on the orphan list: stray orphan-list
  1858. * entries can cause panics at unmount time.
  1859. *
  1860. * This is safe: on error we're going to ignore the orphan list
  1861. * anyway on the next recovery. */
  1862. mem_insert:
  1863. if (!err)
  1864. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1865. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  1866. jbd_debug(4, "orphan inode %lu will point to %d\n",
  1867. inode->i_ino, NEXT_ORPHAN(inode));
  1868. out_unlock:
  1869. mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
  1870. ext4_std_error(inode->i_sb, err);
  1871. return err;
  1872. }
  1873. /*
  1874. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  1875. * of such inodes stored on disk, because it is finally being cleaned up.
  1876. */
  1877. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  1878. {
  1879. struct list_head *prev;
  1880. struct ext4_inode_info *ei = EXT4_I(inode);
  1881. struct ext4_sb_info *sbi;
  1882. __u32 ino_next;
  1883. struct ext4_iloc iloc;
  1884. int err = 0;
  1885. /* ext4_handle_valid() assumes a valid handle_t pointer */
  1886. if (handle && !ext4_handle_valid(handle))
  1887. return 0;
  1888. mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
  1889. if (list_empty(&ei->i_orphan))
  1890. goto out;
  1891. ino_next = NEXT_ORPHAN(inode);
  1892. prev = ei->i_orphan.prev;
  1893. sbi = EXT4_SB(inode->i_sb);
  1894. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  1895. list_del_init(&ei->i_orphan);
  1896. /* If we're on an error path, we may not have a valid
  1897. * transaction handle with which to update the orphan list on
  1898. * disk, but we still need to remove the inode from the linked
  1899. * list in memory. */
  1900. if (sbi->s_journal && !handle)
  1901. goto out;
  1902. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1903. if (err)
  1904. goto out_err;
  1905. if (prev == &sbi->s_orphan) {
  1906. jbd_debug(4, "superblock will point to %u\n", ino_next);
  1907. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1908. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1909. if (err)
  1910. goto out_brelse;
  1911. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  1912. err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
  1913. } else {
  1914. struct ext4_iloc iloc2;
  1915. struct inode *i_prev =
  1916. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  1917. jbd_debug(4, "orphan inode %lu will point to %u\n",
  1918. i_prev->i_ino, ino_next);
  1919. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  1920. if (err)
  1921. goto out_brelse;
  1922. NEXT_ORPHAN(i_prev) = ino_next;
  1923. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  1924. }
  1925. if (err)
  1926. goto out_brelse;
  1927. NEXT_ORPHAN(inode) = 0;
  1928. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1929. out_err:
  1930. ext4_std_error(inode->i_sb, err);
  1931. out:
  1932. mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
  1933. return err;
  1934. out_brelse:
  1935. brelse(iloc.bh);
  1936. goto out_err;
  1937. }
  1938. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  1939. {
  1940. int retval;
  1941. struct inode *inode;
  1942. struct buffer_head *bh;
  1943. struct ext4_dir_entry_2 *de;
  1944. handle_t *handle;
  1945. /* Initialize quotas before so that eventual writes go in
  1946. * separate transaction */
  1947. dquot_initialize(dir);
  1948. dquot_initialize(dentry->d_inode);
  1949. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1950. if (IS_ERR(handle))
  1951. return PTR_ERR(handle);
  1952. retval = -ENOENT;
  1953. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1954. if (!bh)
  1955. goto end_rmdir;
  1956. if (IS_DIRSYNC(dir))
  1957. ext4_handle_sync(handle);
  1958. inode = dentry->d_inode;
  1959. retval = -EIO;
  1960. if (le32_to_cpu(de->inode) != inode->i_ino)
  1961. goto end_rmdir;
  1962. retval = -ENOTEMPTY;
  1963. if (!empty_dir(inode))
  1964. goto end_rmdir;
  1965. retval = ext4_delete_entry(handle, dir, de, bh);
  1966. if (retval)
  1967. goto end_rmdir;
  1968. if (!EXT4_DIR_LINK_EMPTY(inode))
  1969. ext4_warning(inode->i_sb,
  1970. "empty directory has too many links (%d)",
  1971. inode->i_nlink);
  1972. inode->i_version++;
  1973. clear_nlink(inode);
  1974. /* There's no need to set i_disksize: the fact that i_nlink is
  1975. * zero will ensure that the right thing happens during any
  1976. * recovery. */
  1977. inode->i_size = 0;
  1978. ext4_orphan_add(handle, inode);
  1979. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  1980. ext4_mark_inode_dirty(handle, inode);
  1981. ext4_dec_count(handle, dir);
  1982. ext4_update_dx_flag(dir);
  1983. ext4_mark_inode_dirty(handle, dir);
  1984. end_rmdir:
  1985. ext4_journal_stop(handle);
  1986. brelse(bh);
  1987. return retval;
  1988. }
  1989. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  1990. {
  1991. int retval;
  1992. struct inode *inode;
  1993. struct buffer_head *bh;
  1994. struct ext4_dir_entry_2 *de;
  1995. handle_t *handle;
  1996. /* Initialize quotas before so that eventual writes go
  1997. * in separate transaction */
  1998. dquot_initialize(dir);
  1999. dquot_initialize(dentry->d_inode);
  2000. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  2001. if (IS_ERR(handle))
  2002. return PTR_ERR(handle);
  2003. if (IS_DIRSYNC(dir))
  2004. ext4_handle_sync(handle);
  2005. retval = -ENOENT;
  2006. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  2007. if (!bh)
  2008. goto end_unlink;
  2009. inode = dentry->d_inode;
  2010. retval = -EIO;
  2011. if (le32_to_cpu(de->inode) != inode->i_ino)
  2012. goto end_unlink;
  2013. if (!inode->i_nlink) {
  2014. ext4_warning(inode->i_sb,
  2015. "Deleting nonexistent file (%lu), %d",
  2016. inode->i_ino, inode->i_nlink);
  2017. inode->i_nlink = 1;
  2018. }
  2019. retval = ext4_delete_entry(handle, dir, de, bh);
  2020. if (retval)
  2021. goto end_unlink;
  2022. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  2023. ext4_update_dx_flag(dir);
  2024. ext4_mark_inode_dirty(handle, dir);
  2025. drop_nlink(inode);
  2026. if (!inode->i_nlink)
  2027. ext4_orphan_add(handle, inode);
  2028. inode->i_ctime = ext4_current_time(inode);
  2029. ext4_mark_inode_dirty(handle, inode);
  2030. retval = 0;
  2031. end_unlink:
  2032. ext4_journal_stop(handle);
  2033. brelse(bh);
  2034. return retval;
  2035. }
  2036. static int ext4_symlink(struct inode *dir,
  2037. struct dentry *dentry, const char *symname)
  2038. {
  2039. handle_t *handle;
  2040. struct inode *inode;
  2041. int l, err, retries = 0;
  2042. l = strlen(symname)+1;
  2043. if (l > dir->i_sb->s_blocksize)
  2044. return -ENAMETOOLONG;
  2045. dquot_initialize(dir);
  2046. retry:
  2047. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2048. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
  2049. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  2050. if (IS_ERR(handle))
  2051. return PTR_ERR(handle);
  2052. if (IS_DIRSYNC(dir))
  2053. ext4_handle_sync(handle);
  2054. inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
  2055. &dentry->d_name, 0);
  2056. err = PTR_ERR(inode);
  2057. if (IS_ERR(inode))
  2058. goto out_stop;
  2059. if (l > sizeof(EXT4_I(inode)->i_data)) {
  2060. inode->i_op = &ext4_symlink_inode_operations;
  2061. ext4_set_aops(inode);
  2062. /*
  2063. * page_symlink() calls into ext4_prepare/commit_write.
  2064. * We have a transaction open. All is sweetness. It also sets
  2065. * i_size in generic_commit_write().
  2066. */
  2067. err = __page_symlink(inode, symname, l, 1);
  2068. if (err) {
  2069. clear_nlink(inode);
  2070. unlock_new_inode(inode);
  2071. ext4_mark_inode_dirty(handle, inode);
  2072. iput(inode);
  2073. goto out_stop;
  2074. }
  2075. } else {
  2076. /* clear the extent format for fast symlink */
  2077. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  2078. inode->i_op = &ext4_fast_symlink_inode_operations;
  2079. memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
  2080. inode->i_size = l-1;
  2081. }
  2082. EXT4_I(inode)->i_disksize = inode->i_size;
  2083. err = ext4_add_nondir(handle, dentry, inode);
  2084. out_stop:
  2085. ext4_journal_stop(handle);
  2086. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2087. goto retry;
  2088. return err;
  2089. }
  2090. static int ext4_link(struct dentry *old_dentry,
  2091. struct inode *dir, struct dentry *dentry)
  2092. {
  2093. handle_t *handle;
  2094. struct inode *inode = old_dentry->d_inode;
  2095. int err, retries = 0;
  2096. if (inode->i_nlink >= EXT4_LINK_MAX)
  2097. return -EMLINK;
  2098. dquot_initialize(dir);
  2099. retry:
  2100. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2101. EXT4_INDEX_EXTRA_TRANS_BLOCKS);
  2102. if (IS_ERR(handle))
  2103. return PTR_ERR(handle);
  2104. if (IS_DIRSYNC(dir))
  2105. ext4_handle_sync(handle);
  2106. inode->i_ctime = ext4_current_time(inode);
  2107. ext4_inc_count(handle, inode);
  2108. ihold(inode);
  2109. err = ext4_add_entry(handle, dentry, inode);
  2110. if (!err) {
  2111. ext4_mark_inode_dirty(handle, inode);
  2112. d_instantiate(dentry, inode);
  2113. } else {
  2114. drop_nlink(inode);
  2115. iput(inode);
  2116. }
  2117. ext4_journal_stop(handle);
  2118. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2119. goto retry;
  2120. return err;
  2121. }
  2122. #define PARENT_INO(buffer, size) \
  2123. (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
  2124. /*
  2125. * Anybody can rename anything with this: the permission checks are left to the
  2126. * higher-level routines.
  2127. */
  2128. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  2129. struct inode *new_dir, struct dentry *new_dentry)
  2130. {
  2131. handle_t *handle;
  2132. struct inode *old_inode, *new_inode;
  2133. struct buffer_head *old_bh, *new_bh, *dir_bh;
  2134. struct ext4_dir_entry_2 *old_de, *new_de;
  2135. int retval, force_da_alloc = 0;
  2136. dquot_initialize(old_dir);
  2137. dquot_initialize(new_dir);
  2138. old_bh = new_bh = dir_bh = NULL;
  2139. /* Initialize quotas before so that eventual writes go
  2140. * in separate transaction */
  2141. if (new_dentry->d_inode)
  2142. dquot_initialize(new_dentry->d_inode);
  2143. handle = ext4_journal_start(old_dir, 2 *
  2144. EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
  2145. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  2146. if (IS_ERR(handle))
  2147. return PTR_ERR(handle);
  2148. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  2149. ext4_handle_sync(handle);
  2150. old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
  2151. /*
  2152. * Check for inode number is _not_ due to possible IO errors.
  2153. * We might rmdir the source, keep it as pwd of some process
  2154. * and merrily kill the link to whatever was created under the
  2155. * same name. Goodbye sticky bit ;-<
  2156. */
  2157. old_inode = old_dentry->d_inode;
  2158. retval = -ENOENT;
  2159. if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
  2160. goto end_rename;
  2161. new_inode = new_dentry->d_inode;
  2162. new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
  2163. if (new_bh) {
  2164. if (!new_inode) {
  2165. brelse(new_bh);
  2166. new_bh = NULL;
  2167. }
  2168. }
  2169. if (S_ISDIR(old_inode->i_mode)) {
  2170. if (new_inode) {
  2171. retval = -ENOTEMPTY;
  2172. if (!empty_dir(new_inode))
  2173. goto end_rename;
  2174. }
  2175. retval = -EIO;
  2176. dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval);
  2177. if (!dir_bh)
  2178. goto end_rename;
  2179. if (le32_to_cpu(PARENT_INO(dir_bh->b_data,
  2180. old_dir->i_sb->s_blocksize)) != old_dir->i_ino)
  2181. goto end_rename;
  2182. retval = -EMLINK;
  2183. if (!new_inode && new_dir != old_dir &&
  2184. EXT4_DIR_LINK_MAX(new_dir))
  2185. goto end_rename;
  2186. }
  2187. if (!new_bh) {
  2188. retval = ext4_add_entry(handle, new_dentry, old_inode);
  2189. if (retval)
  2190. goto end_rename;
  2191. } else {
  2192. BUFFER_TRACE(new_bh, "get write access");
  2193. ext4_journal_get_write_access(handle, new_bh);
  2194. new_de->inode = cpu_to_le32(old_inode->i_ino);
  2195. if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
  2196. EXT4_FEATURE_INCOMPAT_FILETYPE))
  2197. new_de->file_type = old_de->file_type;
  2198. new_dir->i_version++;
  2199. new_dir->i_ctime = new_dir->i_mtime =
  2200. ext4_current_time(new_dir);
  2201. ext4_mark_inode_dirty(handle, new_dir);
  2202. BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
  2203. retval = ext4_handle_dirty_metadata(handle, new_dir, new_bh);
  2204. if (unlikely(retval)) {
  2205. ext4_std_error(new_dir->i_sb, retval);
  2206. goto end_rename;
  2207. }
  2208. brelse(new_bh);
  2209. new_bh = NULL;
  2210. }
  2211. /*
  2212. * Like most other Unix systems, set the ctime for inodes on a
  2213. * rename.
  2214. */
  2215. old_inode->i_ctime = ext4_current_time(old_inode);
  2216. ext4_mark_inode_dirty(handle, old_inode);
  2217. /*
  2218. * ok, that's it
  2219. */
  2220. if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
  2221. old_de->name_len != old_dentry->d_name.len ||
  2222. strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
  2223. (retval = ext4_delete_entry(handle, old_dir,
  2224. old_de, old_bh)) == -ENOENT) {
  2225. /* old_de could have moved from under us during htree split, so
  2226. * make sure that we are deleting the right entry. We might
  2227. * also be pointing to a stale entry in the unused part of
  2228. * old_bh so just checking inum and the name isn't enough. */
  2229. struct buffer_head *old_bh2;
  2230. struct ext4_dir_entry_2 *old_de2;
  2231. old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
  2232. if (old_bh2) {
  2233. retval = ext4_delete_entry(handle, old_dir,
  2234. old_de2, old_bh2);
  2235. brelse(old_bh2);
  2236. }
  2237. }
  2238. if (retval) {
  2239. ext4_warning(old_dir->i_sb,
  2240. "Deleting old file (%lu), %d, error=%d",
  2241. old_dir->i_ino, old_dir->i_nlink, retval);
  2242. }
  2243. if (new_inode) {
  2244. ext4_dec_count(handle, new_inode);
  2245. new_inode->i_ctime = ext4_current_time(new_inode);
  2246. }
  2247. old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
  2248. ext4_update_dx_flag(old_dir);
  2249. if (dir_bh) {
  2250. BUFFER_TRACE(dir_bh, "get_write_access");
  2251. ext4_journal_get_write_access(handle, dir_bh);
  2252. PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
  2253. cpu_to_le32(new_dir->i_ino);
  2254. BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
  2255. retval = ext4_handle_dirty_metadata(handle, old_dir, dir_bh);
  2256. if (retval) {
  2257. ext4_std_error(old_dir->i_sb, retval);
  2258. goto end_rename;
  2259. }
  2260. ext4_dec_count(handle, old_dir);
  2261. if (new_inode) {
  2262. /* checked empty_dir above, can't have another parent,
  2263. * ext4_dec_count() won't work for many-linked dirs */
  2264. new_inode->i_nlink = 0;
  2265. } else {
  2266. ext4_inc_count(handle, new_dir);
  2267. ext4_update_dx_flag(new_dir);
  2268. ext4_mark_inode_dirty(handle, new_dir);
  2269. }
  2270. }
  2271. ext4_mark_inode_dirty(handle, old_dir);
  2272. if (new_inode) {
  2273. ext4_mark_inode_dirty(handle, new_inode);
  2274. if (!new_inode->i_nlink)
  2275. ext4_orphan_add(handle, new_inode);
  2276. if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
  2277. force_da_alloc = 1;
  2278. }
  2279. retval = 0;
  2280. end_rename:
  2281. brelse(dir_bh);
  2282. brelse(old_bh);
  2283. brelse(new_bh);
  2284. ext4_journal_stop(handle);
  2285. if (retval == 0 && force_da_alloc)
  2286. ext4_alloc_da_blocks(old_inode);
  2287. return retval;
  2288. }
  2289. /*
  2290. * directories can handle most operations...
  2291. */
  2292. const struct inode_operations ext4_dir_inode_operations = {
  2293. .create = ext4_create,
  2294. .lookup = ext4_lookup,
  2295. .link = ext4_link,
  2296. .unlink = ext4_unlink,
  2297. .symlink = ext4_symlink,
  2298. .mkdir = ext4_mkdir,
  2299. .rmdir = ext4_rmdir,
  2300. .mknod = ext4_mknod,
  2301. .rename = ext4_rename,
  2302. .setattr = ext4_setattr,
  2303. #ifdef CONFIG_EXT4_FS_XATTR
  2304. .setxattr = generic_setxattr,
  2305. .getxattr = generic_getxattr,
  2306. .listxattr = ext4_listxattr,
  2307. .removexattr = generic_removexattr,
  2308. #endif
  2309. .check_acl = ext4_check_acl,
  2310. .fiemap = ext4_fiemap,
  2311. };
  2312. const struct inode_operations ext4_special_inode_operations = {
  2313. .setattr = ext4_setattr,
  2314. #ifdef CONFIG_EXT4_FS_XATTR
  2315. .setxattr = generic_setxattr,
  2316. .getxattr = generic_getxattr,
  2317. .listxattr = ext4_listxattr,
  2318. .removexattr = generic_removexattr,
  2319. #endif
  2320. .check_acl = ext4_check_acl,
  2321. };