namei.c 69 KB

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