namei.c 70 KB

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