namei.c 75 KB

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