namei.c 81 KB

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