namei.c 86 KB

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