namei.c 70 KB

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