namei.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/namei.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * from
  11. *
  12. * linux/fs/minix/namei.c
  13. *
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * Big-endian to little-endian byte-swapping/bitmaps by
  17. * David S. Miller (davem@caip.rutgers.edu), 1995
  18. * Directory entry file type support and forward compatibility hooks
  19. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  20. * Hash Tree Directory indexing (c)
  21. * Daniel Phillips, 2001
  22. * Hash Tree Directory indexing porting
  23. * Christopher Li, 2002
  24. * Hash Tree Directory indexing cleanup
  25. * Theodore Ts'o, 2002
  26. */
  27. #include <linux/fs.h>
  28. #include <linux/pagemap.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 <linux/iversion.h>
  37. #include "ext4.h"
  38. #include "ext4_jbd2.h"
  39. #include "xattr.h"
  40. #include "acl.h"
  41. #include <trace/events/ext4.h>
  42. /*
  43. * define how far ahead to read directories while searching them.
  44. */
  45. #define NAMEI_RA_CHUNKS 2
  46. #define NAMEI_RA_BLOCKS 4
  47. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  48. static struct buffer_head *ext4_append(handle_t *handle,
  49. struct inode *inode,
  50. ext4_lblk_t *block)
  51. {
  52. struct buffer_head *bh;
  53. int err;
  54. if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
  55. ((inode->i_size >> 10) >=
  56. EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
  57. return ERR_PTR(-ENOSPC);
  58. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  59. bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
  60. if (IS_ERR(bh))
  61. return bh;
  62. inode->i_size += inode->i_sb->s_blocksize;
  63. EXT4_I(inode)->i_disksize = inode->i_size;
  64. BUFFER_TRACE(bh, "get_write_access");
  65. err = ext4_journal_get_write_access(handle, bh);
  66. if (err) {
  67. brelse(bh);
  68. ext4_std_error(inode->i_sb, err);
  69. return ERR_PTR(err);
  70. }
  71. return bh;
  72. }
  73. static int ext4_dx_csum_verify(struct inode *inode,
  74. struct ext4_dir_entry *dirent);
  75. /*
  76. * Hints to ext4_read_dirblock regarding whether we expect a directory
  77. * block being read to be an index block, or a block containing
  78. * directory entries (and if the latter, whether it was found via a
  79. * logical block in an htree index block). This is used to control
  80. * what sort of sanity checkinig ext4_read_dirblock() will do on the
  81. * directory block read from the storage device. EITHER will means
  82. * the caller doesn't know what kind of directory block will be read,
  83. * so no specific verification will be done.
  84. */
  85. typedef enum {
  86. EITHER, INDEX, DIRENT, DIRENT_HTREE
  87. } dirblock_type_t;
  88. #define ext4_read_dirblock(inode, block, type) \
  89. __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
  90. static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
  91. ext4_lblk_t block,
  92. dirblock_type_t type,
  93. const char *func,
  94. unsigned int line)
  95. {
  96. struct buffer_head *bh;
  97. struct ext4_dir_entry *dirent;
  98. int is_dx_block = 0;
  99. bh = ext4_bread(NULL, inode, block, 0);
  100. if (IS_ERR(bh)) {
  101. __ext4_warning(inode->i_sb, func, line,
  102. "inode #%lu: lblock %lu: comm %s: "
  103. "error %ld reading directory block",
  104. inode->i_ino, (unsigned long)block,
  105. current->comm, PTR_ERR(bh));
  106. return bh;
  107. }
  108. if (!bh && (type == INDEX || type == DIRENT_HTREE)) {
  109. ext4_error_inode(inode, func, line, block,
  110. "Directory hole found for htree %s block",
  111. (type == INDEX) ? "index" : "leaf");
  112. return ERR_PTR(-EFSCORRUPTED);
  113. }
  114. if (!bh)
  115. return NULL;
  116. dirent = (struct ext4_dir_entry *) bh->b_data;
  117. /* Determine whether or not we have an index block */
  118. if (is_dx(inode)) {
  119. if (block == 0)
  120. is_dx_block = 1;
  121. else if (ext4_rec_len_from_disk(dirent->rec_len,
  122. inode->i_sb->s_blocksize) ==
  123. inode->i_sb->s_blocksize)
  124. is_dx_block = 1;
  125. }
  126. if (!is_dx_block && type == INDEX) {
  127. ext4_error_inode(inode, func, line, block,
  128. "directory leaf block found instead of index block");
  129. brelse(bh);
  130. return ERR_PTR(-EFSCORRUPTED);
  131. }
  132. if (!ext4_has_metadata_csum(inode->i_sb) ||
  133. buffer_verified(bh))
  134. return bh;
  135. /*
  136. * An empty leaf block can get mistaken for a index block; for
  137. * this reason, we can only check the index checksum when the
  138. * caller is sure it should be an index block.
  139. */
  140. if (is_dx_block && type == INDEX) {
  141. if (ext4_dx_csum_verify(inode, dirent))
  142. set_buffer_verified(bh);
  143. else {
  144. ext4_error_inode(inode, func, line, block,
  145. "Directory index failed checksum");
  146. brelse(bh);
  147. return ERR_PTR(-EFSBADCRC);
  148. }
  149. }
  150. if (!is_dx_block) {
  151. if (ext4_dirent_csum_verify(inode, dirent))
  152. set_buffer_verified(bh);
  153. else {
  154. ext4_error_inode(inode, func, line, block,
  155. "Directory block failed checksum");
  156. brelse(bh);
  157. return ERR_PTR(-EFSBADCRC);
  158. }
  159. }
  160. return bh;
  161. }
  162. #ifndef assert
  163. #define assert(test) J_ASSERT(test)
  164. #endif
  165. #ifdef DX_DEBUG
  166. #define dxtrace(command) command
  167. #else
  168. #define dxtrace(command)
  169. #endif
  170. struct fake_dirent
  171. {
  172. __le32 inode;
  173. __le16 rec_len;
  174. u8 name_len;
  175. u8 file_type;
  176. };
  177. struct dx_countlimit
  178. {
  179. __le16 limit;
  180. __le16 count;
  181. };
  182. struct dx_entry
  183. {
  184. __le32 hash;
  185. __le32 block;
  186. };
  187. /*
  188. * dx_root_info is laid out so that if it should somehow get overlaid by a
  189. * dirent the two low bits of the hash version will be zero. Therefore, the
  190. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  191. */
  192. struct dx_root
  193. {
  194. struct fake_dirent dot;
  195. char dot_name[4];
  196. struct fake_dirent dotdot;
  197. char dotdot_name[4];
  198. struct dx_root_info
  199. {
  200. __le32 reserved_zero;
  201. u8 hash_version;
  202. u8 info_length; /* 8 */
  203. u8 indirect_levels;
  204. u8 unused_flags;
  205. }
  206. info;
  207. struct dx_entry entries[0];
  208. };
  209. struct dx_node
  210. {
  211. struct fake_dirent fake;
  212. struct dx_entry entries[0];
  213. };
  214. struct dx_frame
  215. {
  216. struct buffer_head *bh;
  217. struct dx_entry *entries;
  218. struct dx_entry *at;
  219. };
  220. struct dx_map_entry
  221. {
  222. u32 hash;
  223. u16 offs;
  224. u16 size;
  225. };
  226. /*
  227. * This goes at the end of each htree block.
  228. */
  229. struct dx_tail {
  230. u32 dt_reserved;
  231. __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
  232. };
  233. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  234. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  235. static inline unsigned dx_get_hash(struct dx_entry *entry);
  236. static void dx_set_hash(struct dx_entry *entry, unsigned value);
  237. static unsigned dx_get_count(struct dx_entry *entries);
  238. static unsigned dx_get_limit(struct dx_entry *entries);
  239. static void dx_set_count(struct dx_entry *entries, unsigned value);
  240. static void dx_set_limit(struct dx_entry *entries, unsigned value);
  241. static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
  242. static unsigned dx_node_limit(struct inode *dir);
  243. static struct dx_frame *dx_probe(struct ext4_filename *fname,
  244. struct inode *dir,
  245. struct dx_hash_info *hinfo,
  246. struct dx_frame *frame);
  247. static void dx_release(struct dx_frame *frames);
  248. static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
  249. unsigned blocksize, struct dx_hash_info *hinfo,
  250. struct dx_map_entry map[]);
  251. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  252. static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
  253. struct dx_map_entry *offsets, int count, unsigned blocksize);
  254. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
  255. static void dx_insert_block(struct dx_frame *frame,
  256. u32 hash, ext4_lblk_t block);
  257. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  258. struct dx_frame *frame,
  259. struct dx_frame *frames,
  260. __u32 *start_hash);
  261. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  262. struct ext4_filename *fname,
  263. struct ext4_dir_entry_2 **res_dir);
  264. static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
  265. struct inode *dir, struct inode *inode);
  266. /* checksumming functions */
  267. void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
  268. unsigned int blocksize)
  269. {
  270. memset(t, 0, sizeof(struct ext4_dir_entry_tail));
  271. t->det_rec_len = ext4_rec_len_to_disk(
  272. sizeof(struct ext4_dir_entry_tail), blocksize);
  273. t->det_reserved_ft = EXT4_FT_DIR_CSUM;
  274. }
  275. /* Walk through a dirent block to find a checksum "dirent" at the tail */
  276. static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
  277. struct ext4_dir_entry *de)
  278. {
  279. struct ext4_dir_entry_tail *t;
  280. #ifdef PARANOID
  281. struct ext4_dir_entry *d, *top;
  282. d = de;
  283. top = (struct ext4_dir_entry *)(((void *)de) +
  284. (EXT4_BLOCK_SIZE(inode->i_sb) -
  285. sizeof(struct ext4_dir_entry_tail)));
  286. while (d < top && d->rec_len)
  287. d = (struct ext4_dir_entry *)(((void *)d) +
  288. le16_to_cpu(d->rec_len));
  289. if (d != top)
  290. return NULL;
  291. t = (struct ext4_dir_entry_tail *)d;
  292. #else
  293. t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
  294. #endif
  295. if (t->det_reserved_zero1 ||
  296. le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
  297. t->det_reserved_zero2 ||
  298. t->det_reserved_ft != EXT4_FT_DIR_CSUM)
  299. return NULL;
  300. return t;
  301. }
  302. static __le32 ext4_dirent_csum(struct inode *inode,
  303. struct ext4_dir_entry *dirent, int size)
  304. {
  305. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  306. struct ext4_inode_info *ei = EXT4_I(inode);
  307. __u32 csum;
  308. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
  309. return cpu_to_le32(csum);
  310. }
  311. #define warn_no_space_for_csum(inode) \
  312. __warn_no_space_for_csum((inode), __func__, __LINE__)
  313. static void __warn_no_space_for_csum(struct inode *inode, const char *func,
  314. unsigned int line)
  315. {
  316. __ext4_warning_inode(inode, func, line,
  317. "No space for directory leaf checksum. Please run e2fsck -D.");
  318. }
  319. int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
  320. {
  321. struct ext4_dir_entry_tail *t;
  322. if (!ext4_has_metadata_csum(inode->i_sb))
  323. return 1;
  324. t = get_dirent_tail(inode, dirent);
  325. if (!t) {
  326. warn_no_space_for_csum(inode);
  327. return 0;
  328. }
  329. if (t->det_checksum != ext4_dirent_csum(inode, dirent,
  330. (void *)t - (void *)dirent))
  331. return 0;
  332. return 1;
  333. }
  334. static void ext4_dirent_csum_set(struct inode *inode,
  335. struct ext4_dir_entry *dirent)
  336. {
  337. struct ext4_dir_entry_tail *t;
  338. if (!ext4_has_metadata_csum(inode->i_sb))
  339. return;
  340. t = get_dirent_tail(inode, dirent);
  341. if (!t) {
  342. warn_no_space_for_csum(inode);
  343. return;
  344. }
  345. t->det_checksum = ext4_dirent_csum(inode, dirent,
  346. (void *)t - (void *)dirent);
  347. }
  348. int ext4_handle_dirty_dirent_node(handle_t *handle,
  349. struct inode *inode,
  350. struct buffer_head *bh)
  351. {
  352. ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  353. return ext4_handle_dirty_metadata(handle, inode, bh);
  354. }
  355. static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
  356. struct ext4_dir_entry *dirent,
  357. int *offset)
  358. {
  359. struct ext4_dir_entry *dp;
  360. struct dx_root_info *root;
  361. int count_offset;
  362. if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
  363. count_offset = 8;
  364. else if (le16_to_cpu(dirent->rec_len) == 12) {
  365. dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
  366. if (le16_to_cpu(dp->rec_len) !=
  367. EXT4_BLOCK_SIZE(inode->i_sb) - 12)
  368. return NULL;
  369. root = (struct dx_root_info *)(((void *)dp + 12));
  370. if (root->reserved_zero ||
  371. root->info_length != sizeof(struct dx_root_info))
  372. return NULL;
  373. count_offset = 32;
  374. } else
  375. return NULL;
  376. if (offset)
  377. *offset = count_offset;
  378. return (struct dx_countlimit *)(((void *)dirent) + count_offset);
  379. }
  380. static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
  381. int count_offset, int count, struct dx_tail *t)
  382. {
  383. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  384. struct ext4_inode_info *ei = EXT4_I(inode);
  385. __u32 csum;
  386. int size;
  387. __u32 dummy_csum = 0;
  388. int offset = offsetof(struct dx_tail, dt_checksum);
  389. size = count_offset + (count * sizeof(struct dx_entry));
  390. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
  391. csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
  392. csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
  393. return cpu_to_le32(csum);
  394. }
  395. static int ext4_dx_csum_verify(struct inode *inode,
  396. struct ext4_dir_entry *dirent)
  397. {
  398. struct dx_countlimit *c;
  399. struct dx_tail *t;
  400. int count_offset, limit, count;
  401. if (!ext4_has_metadata_csum(inode->i_sb))
  402. return 1;
  403. c = get_dx_countlimit(inode, dirent, &count_offset);
  404. if (!c) {
  405. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  406. return 0;
  407. }
  408. limit = le16_to_cpu(c->limit);
  409. count = le16_to_cpu(c->count);
  410. if (count_offset + (limit * sizeof(struct dx_entry)) >
  411. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  412. warn_no_space_for_csum(inode);
  413. return 0;
  414. }
  415. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  416. if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
  417. count, t))
  418. return 0;
  419. return 1;
  420. }
  421. static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
  422. {
  423. struct dx_countlimit *c;
  424. struct dx_tail *t;
  425. int count_offset, limit, count;
  426. if (!ext4_has_metadata_csum(inode->i_sb))
  427. return;
  428. c = get_dx_countlimit(inode, dirent, &count_offset);
  429. if (!c) {
  430. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  431. return;
  432. }
  433. limit = le16_to_cpu(c->limit);
  434. count = le16_to_cpu(c->count);
  435. if (count_offset + (limit * sizeof(struct dx_entry)) >
  436. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  437. warn_no_space_for_csum(inode);
  438. return;
  439. }
  440. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  441. t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
  442. }
  443. static inline int ext4_handle_dirty_dx_node(handle_t *handle,
  444. struct inode *inode,
  445. struct buffer_head *bh)
  446. {
  447. ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  448. return ext4_handle_dirty_metadata(handle, inode, bh);
  449. }
  450. /*
  451. * p is at least 6 bytes before the end of page
  452. */
  453. static inline struct ext4_dir_entry_2 *
  454. ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
  455. {
  456. return (struct ext4_dir_entry_2 *)((char *)p +
  457. ext4_rec_len_from_disk(p->rec_len, blocksize));
  458. }
  459. /*
  460. * Future: use high four bits of block for coalesce-on-delete flags
  461. * Mask them off for now.
  462. */
  463. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  464. {
  465. return le32_to_cpu(entry->block) & 0x0fffffff;
  466. }
  467. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  468. {
  469. entry->block = cpu_to_le32(value);
  470. }
  471. static inline unsigned dx_get_hash(struct dx_entry *entry)
  472. {
  473. return le32_to_cpu(entry->hash);
  474. }
  475. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  476. {
  477. entry->hash = cpu_to_le32(value);
  478. }
  479. static inline unsigned dx_get_count(struct dx_entry *entries)
  480. {
  481. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  482. }
  483. static inline unsigned dx_get_limit(struct dx_entry *entries)
  484. {
  485. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  486. }
  487. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  488. {
  489. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  490. }
  491. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  492. {
  493. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  494. }
  495. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  496. {
  497. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  498. EXT4_DIR_REC_LEN(2) - infosize;
  499. if (ext4_has_metadata_csum(dir->i_sb))
  500. entry_space -= sizeof(struct dx_tail);
  501. return entry_space / sizeof(struct dx_entry);
  502. }
  503. static inline unsigned dx_node_limit(struct inode *dir)
  504. {
  505. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  506. if (ext4_has_metadata_csum(dir->i_sb))
  507. entry_space -= sizeof(struct dx_tail);
  508. return entry_space / sizeof(struct dx_entry);
  509. }
  510. /*
  511. * Debug
  512. */
  513. #ifdef DX_DEBUG
  514. static void dx_show_index(char * label, struct dx_entry *entries)
  515. {
  516. int i, n = dx_get_count (entries);
  517. printk(KERN_DEBUG "%s index", label);
  518. for (i = 0; i < n; i++) {
  519. printk(KERN_CONT " %x->%lu",
  520. i ? dx_get_hash(entries + i) : 0,
  521. (unsigned long)dx_get_block(entries + i));
  522. }
  523. printk(KERN_CONT "\n");
  524. }
  525. struct stats
  526. {
  527. unsigned names;
  528. unsigned space;
  529. unsigned bcount;
  530. };
  531. static struct stats dx_show_leaf(struct inode *dir,
  532. struct dx_hash_info *hinfo,
  533. struct ext4_dir_entry_2 *de,
  534. int size, int show_names)
  535. {
  536. unsigned names = 0, space = 0;
  537. char *base = (char *) de;
  538. struct dx_hash_info h = *hinfo;
  539. printk("names: ");
  540. while ((char *) de < base + size)
  541. {
  542. if (de->inode)
  543. {
  544. if (show_names)
  545. {
  546. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  547. int len;
  548. char *name;
  549. struct fscrypt_str fname_crypto_str =
  550. FSTR_INIT(NULL, 0);
  551. int res = 0;
  552. name = de->name;
  553. len = de->name_len;
  554. if (ext4_encrypted_inode(dir))
  555. res = fscrypt_get_encryption_info(dir);
  556. if (res) {
  557. printk(KERN_WARNING "Error setting up"
  558. " fname crypto: %d\n", res);
  559. }
  560. if (!fscrypt_has_encryption_key(dir)) {
  561. /* Directory is not encrypted */
  562. ext4fs_dirhash(de->name,
  563. de->name_len, &h);
  564. printk("%*.s:(U)%x.%u ", len,
  565. name, h.hash,
  566. (unsigned) ((char *) de
  567. - base));
  568. } else {
  569. struct fscrypt_str de_name =
  570. FSTR_INIT(name, len);
  571. /* Directory is encrypted */
  572. res = fscrypt_fname_alloc_buffer(
  573. dir, len,
  574. &fname_crypto_str);
  575. if (res)
  576. printk(KERN_WARNING "Error "
  577. "allocating crypto "
  578. "buffer--skipping "
  579. "crypto\n");
  580. res = fscrypt_fname_disk_to_usr(dir,
  581. 0, 0, &de_name,
  582. &fname_crypto_str);
  583. if (res) {
  584. printk(KERN_WARNING "Error "
  585. "converting filename "
  586. "from disk to usr"
  587. "\n");
  588. name = "??";
  589. len = 2;
  590. } else {
  591. name = fname_crypto_str.name;
  592. len = fname_crypto_str.len;
  593. }
  594. ext4fs_dirhash(de->name, de->name_len,
  595. &h);
  596. printk("%*.s:(E)%x.%u ", len, name,
  597. h.hash, (unsigned) ((char *) de
  598. - base));
  599. fscrypt_fname_free_buffer(
  600. &fname_crypto_str);
  601. }
  602. #else
  603. int len = de->name_len;
  604. char *name = de->name;
  605. ext4fs_dirhash(de->name, de->name_len, &h);
  606. printk("%*.s:%x.%u ", len, name, h.hash,
  607. (unsigned) ((char *) de - base));
  608. #endif
  609. }
  610. space += EXT4_DIR_REC_LEN(de->name_len);
  611. names++;
  612. }
  613. de = ext4_next_entry(de, size);
  614. }
  615. printk(KERN_CONT "(%i)\n", names);
  616. return (struct stats) { names, space, 1 };
  617. }
  618. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  619. struct dx_entry *entries, int levels)
  620. {
  621. unsigned blocksize = dir->i_sb->s_blocksize;
  622. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  623. unsigned bcount = 0;
  624. struct buffer_head *bh;
  625. printk("%i indexed blocks...\n", count);
  626. for (i = 0; i < count; i++, entries++)
  627. {
  628. ext4_lblk_t block = dx_get_block(entries);
  629. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  630. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  631. struct stats stats;
  632. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  633. bh = ext4_bread(NULL,dir, block, 0);
  634. if (!bh || IS_ERR(bh))
  635. continue;
  636. stats = levels?
  637. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  638. dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
  639. bh->b_data, blocksize, 0);
  640. names += stats.names;
  641. space += stats.space;
  642. bcount += stats.bcount;
  643. brelse(bh);
  644. }
  645. if (bcount)
  646. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  647. levels ? "" : " ", names, space/bcount,
  648. (space/bcount)*100/blocksize);
  649. return (struct stats) { names, space, bcount};
  650. }
  651. #endif /* DX_DEBUG */
  652. /*
  653. * Probe for a directory leaf block to search.
  654. *
  655. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  656. * error in the directory index, and the caller should fall back to
  657. * searching the directory normally. The callers of dx_probe **MUST**
  658. * check for this error code, and make sure it never gets reflected
  659. * back to userspace.
  660. */
  661. static struct dx_frame *
  662. dx_probe(struct ext4_filename *fname, struct inode *dir,
  663. struct dx_hash_info *hinfo, struct dx_frame *frame_in)
  664. {
  665. unsigned count, indirect;
  666. struct dx_entry *at, *entries, *p, *q, *m;
  667. struct dx_root *root;
  668. struct dx_frame *frame = frame_in;
  669. struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
  670. u32 hash;
  671. memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
  672. frame->bh = ext4_read_dirblock(dir, 0, INDEX);
  673. if (IS_ERR(frame->bh))
  674. return (struct dx_frame *) frame->bh;
  675. root = (struct dx_root *) frame->bh->b_data;
  676. if (root->info.hash_version != DX_HASH_TEA &&
  677. root->info.hash_version != DX_HASH_HALF_MD4 &&
  678. root->info.hash_version != DX_HASH_LEGACY) {
  679. ext4_warning_inode(dir, "Unrecognised inode hash code %u",
  680. root->info.hash_version);
  681. goto fail;
  682. }
  683. if (fname)
  684. hinfo = &fname->hinfo;
  685. hinfo->hash_version = root->info.hash_version;
  686. if (hinfo->hash_version <= DX_HASH_TEA)
  687. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  688. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  689. if (fname && fname_name(fname))
  690. ext4fs_dirhash(fname_name(fname), fname_len(fname), hinfo);
  691. hash = hinfo->hash;
  692. if (root->info.unused_flags & 1) {
  693. ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
  694. root->info.unused_flags);
  695. goto fail;
  696. }
  697. indirect = root->info.indirect_levels;
  698. if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
  699. ext4_warning(dir->i_sb,
  700. "Directory (ino: %lu) htree depth %#06x exceed"
  701. "supported value", dir->i_ino,
  702. ext4_dir_htree_level(dir->i_sb));
  703. if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
  704. ext4_warning(dir->i_sb, "Enable large directory "
  705. "feature to access it");
  706. }
  707. goto fail;
  708. }
  709. entries = (struct dx_entry *)(((char *)&root->info) +
  710. root->info.info_length);
  711. if (dx_get_limit(entries) != dx_root_limit(dir,
  712. root->info.info_length)) {
  713. ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
  714. dx_get_limit(entries),
  715. dx_root_limit(dir, root->info.info_length));
  716. goto fail;
  717. }
  718. dxtrace(printk("Look up %x", hash));
  719. while (1) {
  720. count = dx_get_count(entries);
  721. if (!count || count > dx_get_limit(entries)) {
  722. ext4_warning_inode(dir,
  723. "dx entry: count %u beyond limit %u",
  724. count, dx_get_limit(entries));
  725. goto fail;
  726. }
  727. p = entries + 1;
  728. q = entries + count - 1;
  729. while (p <= q) {
  730. m = p + (q - p) / 2;
  731. dxtrace(printk(KERN_CONT "."));
  732. if (dx_get_hash(m) > hash)
  733. q = m - 1;
  734. else
  735. p = m + 1;
  736. }
  737. if (0) { // linear search cross check
  738. unsigned n = count - 1;
  739. at = entries;
  740. while (n--)
  741. {
  742. dxtrace(printk(KERN_CONT ","));
  743. if (dx_get_hash(++at) > hash)
  744. {
  745. at--;
  746. break;
  747. }
  748. }
  749. assert (at == p - 1);
  750. }
  751. at = p - 1;
  752. dxtrace(printk(KERN_CONT " %x->%u\n",
  753. at == entries ? 0 : dx_get_hash(at),
  754. dx_get_block(at)));
  755. frame->entries = entries;
  756. frame->at = at;
  757. if (!indirect--)
  758. return frame;
  759. frame++;
  760. frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
  761. if (IS_ERR(frame->bh)) {
  762. ret_err = (struct dx_frame *) frame->bh;
  763. frame->bh = NULL;
  764. goto fail;
  765. }
  766. entries = ((struct dx_node *) frame->bh->b_data)->entries;
  767. if (dx_get_limit(entries) != dx_node_limit(dir)) {
  768. ext4_warning_inode(dir,
  769. "dx entry: limit %u != node limit %u",
  770. dx_get_limit(entries), dx_node_limit(dir));
  771. goto fail;
  772. }
  773. }
  774. fail:
  775. while (frame >= frame_in) {
  776. brelse(frame->bh);
  777. frame--;
  778. }
  779. if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
  780. ext4_warning_inode(dir,
  781. "Corrupt directory, running e2fsck is recommended");
  782. return ret_err;
  783. }
  784. static void dx_release(struct dx_frame *frames)
  785. {
  786. struct dx_root_info *info;
  787. int i;
  788. unsigned int indirect_levels;
  789. if (frames[0].bh == NULL)
  790. return;
  791. info = &((struct dx_root *)frames[0].bh->b_data)->info;
  792. /* save local copy, "info" may be freed after brelse() */
  793. indirect_levels = info->indirect_levels;
  794. for (i = 0; i <= indirect_levels; i++) {
  795. if (frames[i].bh == NULL)
  796. break;
  797. brelse(frames[i].bh);
  798. frames[i].bh = NULL;
  799. }
  800. }
  801. /*
  802. * This function increments the frame pointer to search the next leaf
  803. * block, and reads in the necessary intervening nodes if the search
  804. * should be necessary. Whether or not the search is necessary is
  805. * controlled by the hash parameter. If the hash value is even, then
  806. * the search is only continued if the next block starts with that
  807. * hash value. This is used if we are searching for a specific file.
  808. *
  809. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  810. *
  811. * This function returns 1 if the caller should continue to search,
  812. * or 0 if it should not. If there is an error reading one of the
  813. * index blocks, it will a negative error code.
  814. *
  815. * If start_hash is non-null, it will be filled in with the starting
  816. * hash of the next page.
  817. */
  818. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  819. struct dx_frame *frame,
  820. struct dx_frame *frames,
  821. __u32 *start_hash)
  822. {
  823. struct dx_frame *p;
  824. struct buffer_head *bh;
  825. int num_frames = 0;
  826. __u32 bhash;
  827. p = frame;
  828. /*
  829. * Find the next leaf page by incrementing the frame pointer.
  830. * If we run out of entries in the interior node, loop around and
  831. * increment pointer in the parent node. When we break out of
  832. * this loop, num_frames indicates the number of interior
  833. * nodes need to be read.
  834. */
  835. while (1) {
  836. if (++(p->at) < p->entries + dx_get_count(p->entries))
  837. break;
  838. if (p == frames)
  839. return 0;
  840. num_frames++;
  841. p--;
  842. }
  843. /*
  844. * If the hash is 1, then continue only if the next page has a
  845. * continuation hash of any value. This is used for readdir
  846. * handling. Otherwise, check to see if the hash matches the
  847. * desired contiuation hash. If it doesn't, return since
  848. * there's no point to read in the successive index pages.
  849. */
  850. bhash = dx_get_hash(p->at);
  851. if (start_hash)
  852. *start_hash = bhash;
  853. if ((hash & 1) == 0) {
  854. if ((bhash & ~1) != hash)
  855. return 0;
  856. }
  857. /*
  858. * If the hash is HASH_NB_ALWAYS, we always go to the next
  859. * block so no check is necessary
  860. */
  861. while (num_frames--) {
  862. bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
  863. if (IS_ERR(bh))
  864. return PTR_ERR(bh);
  865. p++;
  866. brelse(p->bh);
  867. p->bh = bh;
  868. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  869. }
  870. return 1;
  871. }
  872. /*
  873. * This function fills a red-black tree with information from a
  874. * directory block. It returns the number directory entries loaded
  875. * into the tree. If there is an error it is returned in err.
  876. */
  877. static int htree_dirblock_to_tree(struct file *dir_file,
  878. struct inode *dir, ext4_lblk_t block,
  879. struct dx_hash_info *hinfo,
  880. __u32 start_hash, __u32 start_minor_hash)
  881. {
  882. struct buffer_head *bh;
  883. struct ext4_dir_entry_2 *de, *top;
  884. int err = 0, count = 0;
  885. struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
  886. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  887. (unsigned long)block));
  888. bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
  889. if (IS_ERR(bh))
  890. return PTR_ERR(bh);
  891. de = (struct ext4_dir_entry_2 *) bh->b_data;
  892. top = (struct ext4_dir_entry_2 *) ((char *) de +
  893. dir->i_sb->s_blocksize -
  894. EXT4_DIR_REC_LEN(0));
  895. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  896. /* Check if the directory is encrypted */
  897. if (ext4_encrypted_inode(dir)) {
  898. err = fscrypt_get_encryption_info(dir);
  899. if (err < 0) {
  900. brelse(bh);
  901. return err;
  902. }
  903. err = fscrypt_fname_alloc_buffer(dir, EXT4_NAME_LEN,
  904. &fname_crypto_str);
  905. if (err < 0) {
  906. brelse(bh);
  907. return err;
  908. }
  909. }
  910. #endif
  911. for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
  912. if (ext4_check_dir_entry(dir, NULL, de, bh,
  913. bh->b_data, bh->b_size,
  914. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  915. + ((char *)de - bh->b_data))) {
  916. /* silently ignore the rest of the block */
  917. break;
  918. }
  919. ext4fs_dirhash(de->name, de->name_len, hinfo);
  920. if ((hinfo->hash < start_hash) ||
  921. ((hinfo->hash == start_hash) &&
  922. (hinfo->minor_hash < start_minor_hash)))
  923. continue;
  924. if (de->inode == 0)
  925. continue;
  926. if (!ext4_encrypted_inode(dir)) {
  927. tmp_str.name = de->name;
  928. tmp_str.len = de->name_len;
  929. err = ext4_htree_store_dirent(dir_file,
  930. hinfo->hash, hinfo->minor_hash, de,
  931. &tmp_str);
  932. } else {
  933. int save_len = fname_crypto_str.len;
  934. struct fscrypt_str de_name = FSTR_INIT(de->name,
  935. de->name_len);
  936. /* Directory is encrypted */
  937. err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
  938. hinfo->minor_hash, &de_name,
  939. &fname_crypto_str);
  940. if (err) {
  941. count = err;
  942. goto errout;
  943. }
  944. err = ext4_htree_store_dirent(dir_file,
  945. hinfo->hash, hinfo->minor_hash, de,
  946. &fname_crypto_str);
  947. fname_crypto_str.len = save_len;
  948. }
  949. if (err != 0) {
  950. count = err;
  951. goto errout;
  952. }
  953. count++;
  954. }
  955. errout:
  956. brelse(bh);
  957. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  958. fscrypt_fname_free_buffer(&fname_crypto_str);
  959. #endif
  960. return count;
  961. }
  962. /*
  963. * This function fills a red-black tree with information from a
  964. * directory. We start scanning the directory in hash order, starting
  965. * at start_hash and start_minor_hash.
  966. *
  967. * This function returns the number of entries inserted into the tree,
  968. * or a negative error code.
  969. */
  970. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  971. __u32 start_minor_hash, __u32 *next_hash)
  972. {
  973. struct dx_hash_info hinfo;
  974. struct ext4_dir_entry_2 *de;
  975. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  976. struct inode *dir;
  977. ext4_lblk_t block;
  978. int count = 0;
  979. int ret, err;
  980. __u32 hashval;
  981. struct fscrypt_str tmp_str;
  982. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  983. start_hash, start_minor_hash));
  984. dir = file_inode(dir_file);
  985. if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
  986. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  987. if (hinfo.hash_version <= DX_HASH_TEA)
  988. hinfo.hash_version +=
  989. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  990. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  991. if (ext4_has_inline_data(dir)) {
  992. int has_inline_data = 1;
  993. count = htree_inlinedir_to_tree(dir_file, dir, 0,
  994. &hinfo, start_hash,
  995. start_minor_hash,
  996. &has_inline_data);
  997. if (has_inline_data) {
  998. *next_hash = ~0;
  999. return count;
  1000. }
  1001. }
  1002. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  1003. start_hash, start_minor_hash);
  1004. *next_hash = ~0;
  1005. return count;
  1006. }
  1007. hinfo.hash = start_hash;
  1008. hinfo.minor_hash = 0;
  1009. frame = dx_probe(NULL, dir, &hinfo, frames);
  1010. if (IS_ERR(frame))
  1011. return PTR_ERR(frame);
  1012. /* Add '.' and '..' from the htree header */
  1013. if (!start_hash && !start_minor_hash) {
  1014. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  1015. tmp_str.name = de->name;
  1016. tmp_str.len = de->name_len;
  1017. err = ext4_htree_store_dirent(dir_file, 0, 0,
  1018. de, &tmp_str);
  1019. if (err != 0)
  1020. goto errout;
  1021. count++;
  1022. }
  1023. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  1024. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  1025. de = ext4_next_entry(de, dir->i_sb->s_blocksize);
  1026. tmp_str.name = de->name;
  1027. tmp_str.len = de->name_len;
  1028. err = ext4_htree_store_dirent(dir_file, 2, 0,
  1029. de, &tmp_str);
  1030. if (err != 0)
  1031. goto errout;
  1032. count++;
  1033. }
  1034. while (1) {
  1035. if (fatal_signal_pending(current)) {
  1036. err = -ERESTARTSYS;
  1037. goto errout;
  1038. }
  1039. cond_resched();
  1040. block = dx_get_block(frame->at);
  1041. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  1042. start_hash, start_minor_hash);
  1043. if (ret < 0) {
  1044. err = ret;
  1045. goto errout;
  1046. }
  1047. count += ret;
  1048. hashval = ~0;
  1049. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  1050. frame, frames, &hashval);
  1051. *next_hash = hashval;
  1052. if (ret < 0) {
  1053. err = ret;
  1054. goto errout;
  1055. }
  1056. /*
  1057. * Stop if: (a) there are no more entries, or
  1058. * (b) we have inserted at least one entry and the
  1059. * next hash value is not a continuation
  1060. */
  1061. if ((ret == 0) ||
  1062. (count && ((hashval & 1) == 0)))
  1063. break;
  1064. }
  1065. dx_release(frames);
  1066. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  1067. "next hash: %x\n", count, *next_hash));
  1068. return count;
  1069. errout:
  1070. dx_release(frames);
  1071. return (err);
  1072. }
  1073. static inline int search_dirblock(struct buffer_head *bh,
  1074. struct inode *dir,
  1075. struct ext4_filename *fname,
  1076. unsigned int offset,
  1077. struct ext4_dir_entry_2 **res_dir)
  1078. {
  1079. return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
  1080. fname, offset, res_dir);
  1081. }
  1082. /*
  1083. * Directory block splitting, compacting
  1084. */
  1085. /*
  1086. * Create map of hash values, offsets, and sizes, stored at end of block.
  1087. * Returns number of entries mapped.
  1088. */
  1089. static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
  1090. unsigned blocksize, struct dx_hash_info *hinfo,
  1091. struct dx_map_entry *map_tail)
  1092. {
  1093. int count = 0;
  1094. char *base = (char *) de;
  1095. struct dx_hash_info h = *hinfo;
  1096. while ((char *) de < base + blocksize) {
  1097. if (de->name_len && de->inode) {
  1098. ext4fs_dirhash(de->name, de->name_len, &h);
  1099. map_tail--;
  1100. map_tail->hash = h.hash;
  1101. map_tail->offs = ((char *) de - base)>>2;
  1102. map_tail->size = le16_to_cpu(de->rec_len);
  1103. count++;
  1104. cond_resched();
  1105. }
  1106. /* XXX: do we need to check rec_len == 0 case? -Chris */
  1107. de = ext4_next_entry(de, blocksize);
  1108. }
  1109. return count;
  1110. }
  1111. /* Sort map by hash value */
  1112. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  1113. {
  1114. struct dx_map_entry *p, *q, *top = map + count - 1;
  1115. int more;
  1116. /* Combsort until bubble sort doesn't suck */
  1117. while (count > 2) {
  1118. count = count*10/13;
  1119. if (count - 9 < 2) /* 9, 10 -> 11 */
  1120. count = 11;
  1121. for (p = top, q = p - count; q >= map; p--, q--)
  1122. if (p->hash < q->hash)
  1123. swap(*p, *q);
  1124. }
  1125. /* Garden variety bubble sort */
  1126. do {
  1127. more = 0;
  1128. q = top;
  1129. while (q-- > map) {
  1130. if (q[1].hash >= q[0].hash)
  1131. continue;
  1132. swap(*(q+1), *q);
  1133. more = 1;
  1134. }
  1135. } while(more);
  1136. }
  1137. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  1138. {
  1139. struct dx_entry *entries = frame->entries;
  1140. struct dx_entry *old = frame->at, *new = old + 1;
  1141. int count = dx_get_count(entries);
  1142. assert(count < dx_get_limit(entries));
  1143. assert(old < entries + count);
  1144. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  1145. dx_set_hash(new, hash);
  1146. dx_set_block(new, block);
  1147. dx_set_count(entries, count + 1);
  1148. }
  1149. /*
  1150. * Test whether a directory entry matches the filename being searched for.
  1151. *
  1152. * Return: %true if the directory entry matches, otherwise %false.
  1153. */
  1154. static inline bool ext4_match(const struct ext4_filename *fname,
  1155. const struct ext4_dir_entry_2 *de)
  1156. {
  1157. struct fscrypt_name f;
  1158. if (!de->inode)
  1159. return false;
  1160. f.usr_fname = fname->usr_fname;
  1161. f.disk_name = fname->disk_name;
  1162. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1163. f.crypto_buf = fname->crypto_buf;
  1164. #endif
  1165. return fscrypt_match_name(&f, de->name, de->name_len);
  1166. }
  1167. /*
  1168. * Returns 0 if not found, -1 on failure, and 1 on success
  1169. */
  1170. int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
  1171. struct inode *dir, struct ext4_filename *fname,
  1172. unsigned int offset, struct ext4_dir_entry_2 **res_dir)
  1173. {
  1174. struct ext4_dir_entry_2 * de;
  1175. char * dlimit;
  1176. int de_len;
  1177. de = (struct ext4_dir_entry_2 *)search_buf;
  1178. dlimit = search_buf + buf_size;
  1179. while ((char *) de < dlimit) {
  1180. /* this code is executed quadratically often */
  1181. /* do minimal checking `by hand' */
  1182. if ((char *) de + de->name_len <= dlimit &&
  1183. ext4_match(fname, de)) {
  1184. /* found a match - just to be sure, do
  1185. * a full check */
  1186. if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
  1187. bh->b_size, offset))
  1188. return -1;
  1189. *res_dir = de;
  1190. return 1;
  1191. }
  1192. /* prevent looping on a bad block */
  1193. de_len = ext4_rec_len_from_disk(de->rec_len,
  1194. dir->i_sb->s_blocksize);
  1195. if (de_len <= 0)
  1196. return -1;
  1197. offset += de_len;
  1198. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  1199. }
  1200. return 0;
  1201. }
  1202. static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
  1203. struct ext4_dir_entry *de)
  1204. {
  1205. struct super_block *sb = dir->i_sb;
  1206. if (!is_dx(dir))
  1207. return 0;
  1208. if (block == 0)
  1209. return 1;
  1210. if (de->inode == 0 &&
  1211. ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
  1212. sb->s_blocksize)
  1213. return 1;
  1214. return 0;
  1215. }
  1216. /*
  1217. * ext4_find_entry()
  1218. *
  1219. * finds an entry in the specified directory with the wanted name. It
  1220. * returns the cache buffer in which the entry was found, and the entry
  1221. * itself (as a parameter - res_dir). It does NOT read the inode of the
  1222. * entry - you'll have to do that yourself if you want to.
  1223. *
  1224. * The returned buffer_head has ->b_count elevated. The caller is expected
  1225. * to brelse() it when appropriate.
  1226. */
  1227. static struct buffer_head * ext4_find_entry (struct inode *dir,
  1228. const struct qstr *d_name,
  1229. struct ext4_dir_entry_2 **res_dir,
  1230. int *inlined)
  1231. {
  1232. struct super_block *sb;
  1233. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  1234. struct buffer_head *bh, *ret = NULL;
  1235. ext4_lblk_t start, block;
  1236. const u8 *name = d_name->name;
  1237. size_t ra_max = 0; /* Number of bh's in the readahead
  1238. buffer, bh_use[] */
  1239. size_t ra_ptr = 0; /* Current index into readahead
  1240. buffer */
  1241. ext4_lblk_t nblocks;
  1242. int i, namelen, retval;
  1243. struct ext4_filename fname;
  1244. *res_dir = NULL;
  1245. sb = dir->i_sb;
  1246. namelen = d_name->len;
  1247. if (namelen > EXT4_NAME_LEN)
  1248. return NULL;
  1249. retval = ext4_fname_setup_filename(dir, d_name, 1, &fname);
  1250. if (retval == -ENOENT)
  1251. return NULL;
  1252. if (retval)
  1253. return ERR_PTR(retval);
  1254. if (ext4_has_inline_data(dir)) {
  1255. int has_inline_data = 1;
  1256. ret = ext4_find_inline_entry(dir, &fname, res_dir,
  1257. &has_inline_data);
  1258. if (has_inline_data) {
  1259. if (inlined)
  1260. *inlined = 1;
  1261. goto cleanup_and_exit;
  1262. }
  1263. }
  1264. if ((namelen <= 2) && (name[0] == '.') &&
  1265. (name[1] == '.' || name[1] == '\0')) {
  1266. /*
  1267. * "." or ".." will only be in the first block
  1268. * NFS may look up ".."; "." should be handled by the VFS
  1269. */
  1270. block = start = 0;
  1271. nblocks = 1;
  1272. goto restart;
  1273. }
  1274. if (is_dx(dir)) {
  1275. ret = ext4_dx_find_entry(dir, &fname, res_dir);
  1276. /*
  1277. * On success, or if the error was file not found,
  1278. * return. Otherwise, fall back to doing a search the
  1279. * old fashioned way.
  1280. */
  1281. if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
  1282. goto cleanup_and_exit;
  1283. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  1284. "falling back\n"));
  1285. ret = NULL;
  1286. }
  1287. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1288. if (!nblocks) {
  1289. ret = NULL;
  1290. goto cleanup_and_exit;
  1291. }
  1292. start = EXT4_I(dir)->i_dir_start_lookup;
  1293. if (start >= nblocks)
  1294. start = 0;
  1295. block = start;
  1296. restart:
  1297. do {
  1298. /*
  1299. * We deal with the read-ahead logic here.
  1300. */
  1301. cond_resched();
  1302. if (ra_ptr >= ra_max) {
  1303. /* Refill the readahead buffer */
  1304. ra_ptr = 0;
  1305. if (block < start)
  1306. ra_max = start - block;
  1307. else
  1308. ra_max = nblocks - block;
  1309. ra_max = min(ra_max, ARRAY_SIZE(bh_use));
  1310. retval = ext4_bread_batch(dir, block, ra_max,
  1311. false /* wait */, bh_use);
  1312. if (retval) {
  1313. ret = ERR_PTR(retval);
  1314. ra_max = 0;
  1315. goto cleanup_and_exit;
  1316. }
  1317. }
  1318. if ((bh = bh_use[ra_ptr++]) == NULL)
  1319. goto next;
  1320. wait_on_buffer(bh);
  1321. if (!buffer_uptodate(bh)) {
  1322. EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
  1323. (unsigned long) block);
  1324. brelse(bh);
  1325. ret = ERR_PTR(-EIO);
  1326. goto cleanup_and_exit;
  1327. }
  1328. if (!buffer_verified(bh) &&
  1329. !is_dx_internal_node(dir, block,
  1330. (struct ext4_dir_entry *)bh->b_data) &&
  1331. !ext4_dirent_csum_verify(dir,
  1332. (struct ext4_dir_entry *)bh->b_data)) {
  1333. EXT4_ERROR_INODE(dir, "checksumming directory "
  1334. "block %lu", (unsigned long)block);
  1335. brelse(bh);
  1336. ret = ERR_PTR(-EFSBADCRC);
  1337. goto cleanup_and_exit;
  1338. }
  1339. set_buffer_verified(bh);
  1340. i = search_dirblock(bh, dir, &fname,
  1341. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  1342. if (i == 1) {
  1343. EXT4_I(dir)->i_dir_start_lookup = block;
  1344. ret = bh;
  1345. goto cleanup_and_exit;
  1346. } else {
  1347. brelse(bh);
  1348. if (i < 0)
  1349. goto cleanup_and_exit;
  1350. }
  1351. next:
  1352. if (++block >= nblocks)
  1353. block = 0;
  1354. } while (block != start);
  1355. /*
  1356. * If the directory has grown while we were searching, then
  1357. * search the last part of the directory before giving up.
  1358. */
  1359. block = nblocks;
  1360. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1361. if (block < nblocks) {
  1362. start = 0;
  1363. goto restart;
  1364. }
  1365. cleanup_and_exit:
  1366. /* Clean up the read-ahead blocks */
  1367. for (; ra_ptr < ra_max; ra_ptr++)
  1368. brelse(bh_use[ra_ptr]);
  1369. ext4_fname_free_filename(&fname);
  1370. return ret;
  1371. }
  1372. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  1373. struct ext4_filename *fname,
  1374. struct ext4_dir_entry_2 **res_dir)
  1375. {
  1376. struct super_block * sb = dir->i_sb;
  1377. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  1378. struct buffer_head *bh;
  1379. ext4_lblk_t block;
  1380. int retval;
  1381. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1382. *res_dir = NULL;
  1383. #endif
  1384. frame = dx_probe(fname, dir, NULL, frames);
  1385. if (IS_ERR(frame))
  1386. return (struct buffer_head *) frame;
  1387. do {
  1388. block = dx_get_block(frame->at);
  1389. bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
  1390. if (IS_ERR(bh))
  1391. goto errout;
  1392. retval = search_dirblock(bh, dir, fname,
  1393. block << EXT4_BLOCK_SIZE_BITS(sb),
  1394. res_dir);
  1395. if (retval == 1)
  1396. goto success;
  1397. brelse(bh);
  1398. if (retval == -1) {
  1399. bh = ERR_PTR(ERR_BAD_DX_DIR);
  1400. goto errout;
  1401. }
  1402. /* Check to see if we should continue to search */
  1403. retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
  1404. frames, NULL);
  1405. if (retval < 0) {
  1406. ext4_warning_inode(dir,
  1407. "error %d reading directory index block",
  1408. retval);
  1409. bh = ERR_PTR(retval);
  1410. goto errout;
  1411. }
  1412. } while (retval == 1);
  1413. bh = NULL;
  1414. errout:
  1415. dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
  1416. success:
  1417. dx_release(frames);
  1418. return bh;
  1419. }
  1420. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  1421. {
  1422. struct inode *inode;
  1423. struct ext4_dir_entry_2 *de;
  1424. struct buffer_head *bh;
  1425. int err;
  1426. err = fscrypt_prepare_lookup(dir, dentry, flags);
  1427. if (err)
  1428. return ERR_PTR(err);
  1429. if (dentry->d_name.len > EXT4_NAME_LEN)
  1430. return ERR_PTR(-ENAMETOOLONG);
  1431. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  1432. if (IS_ERR(bh))
  1433. return (struct dentry *) bh;
  1434. inode = NULL;
  1435. if (bh) {
  1436. __u32 ino = le32_to_cpu(de->inode);
  1437. brelse(bh);
  1438. if (!ext4_valid_inum(dir->i_sb, ino)) {
  1439. EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
  1440. return ERR_PTR(-EFSCORRUPTED);
  1441. }
  1442. if (unlikely(ino == dir->i_ino)) {
  1443. EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
  1444. dentry);
  1445. return ERR_PTR(-EFSCORRUPTED);
  1446. }
  1447. inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL);
  1448. if (inode == ERR_PTR(-ESTALE)) {
  1449. EXT4_ERROR_INODE(dir,
  1450. "deleted inode referenced: %u",
  1451. ino);
  1452. return ERR_PTR(-EFSCORRUPTED);
  1453. }
  1454. if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
  1455. (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
  1456. !fscrypt_has_permitted_context(dir, inode)) {
  1457. ext4_warning(inode->i_sb,
  1458. "Inconsistent encryption contexts: %lu/%lu",
  1459. dir->i_ino, inode->i_ino);
  1460. iput(inode);
  1461. return ERR_PTR(-EPERM);
  1462. }
  1463. }
  1464. return d_splice_alias(inode, dentry);
  1465. }
  1466. struct dentry *ext4_get_parent(struct dentry *child)
  1467. {
  1468. __u32 ino;
  1469. static const struct qstr dotdot = QSTR_INIT("..", 2);
  1470. struct ext4_dir_entry_2 * de;
  1471. struct buffer_head *bh;
  1472. bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
  1473. if (IS_ERR(bh))
  1474. return (struct dentry *) bh;
  1475. if (!bh)
  1476. return ERR_PTR(-ENOENT);
  1477. ino = le32_to_cpu(de->inode);
  1478. brelse(bh);
  1479. if (!ext4_valid_inum(child->d_sb, ino)) {
  1480. EXT4_ERROR_INODE(d_inode(child),
  1481. "bad parent inode number: %u", ino);
  1482. return ERR_PTR(-EFSCORRUPTED);
  1483. }
  1484. return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL));
  1485. }
  1486. /*
  1487. * Move count entries from end of map between two memory locations.
  1488. * Returns pointer to last entry moved.
  1489. */
  1490. static struct ext4_dir_entry_2 *
  1491. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
  1492. unsigned blocksize)
  1493. {
  1494. unsigned rec_len = 0;
  1495. while (count--) {
  1496. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1497. (from + (map->offs<<2));
  1498. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1499. memcpy (to, de, rec_len);
  1500. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1501. ext4_rec_len_to_disk(rec_len, blocksize);
  1502. de->inode = 0;
  1503. map++;
  1504. to += rec_len;
  1505. }
  1506. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1507. }
  1508. /*
  1509. * Compact each dir entry in the range to the minimal rec_len.
  1510. * Returns pointer to last entry in range.
  1511. */
  1512. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
  1513. {
  1514. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1515. unsigned rec_len = 0;
  1516. prev = to = de;
  1517. while ((char*)de < base + blocksize) {
  1518. next = ext4_next_entry(de, blocksize);
  1519. if (de->inode && de->name_len) {
  1520. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1521. if (de > to)
  1522. memmove(to, de, rec_len);
  1523. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1524. prev = to;
  1525. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1526. }
  1527. de = next;
  1528. }
  1529. return prev;
  1530. }
  1531. /*
  1532. * Split a full leaf block to make room for a new dir entry.
  1533. * Allocate a new block, and move entries so that they are approx. equally full.
  1534. * Returns pointer to de in block into which the new entry will be inserted.
  1535. */
  1536. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1537. struct buffer_head **bh,struct dx_frame *frame,
  1538. struct dx_hash_info *hinfo)
  1539. {
  1540. unsigned blocksize = dir->i_sb->s_blocksize;
  1541. unsigned count, continued;
  1542. struct buffer_head *bh2;
  1543. ext4_lblk_t newblock;
  1544. u32 hash2;
  1545. struct dx_map_entry *map;
  1546. char *data1 = (*bh)->b_data, *data2;
  1547. unsigned split, move, size;
  1548. struct ext4_dir_entry_2 *de = NULL, *de2;
  1549. struct ext4_dir_entry_tail *t;
  1550. int csum_size = 0;
  1551. int err = 0, i;
  1552. if (ext4_has_metadata_csum(dir->i_sb))
  1553. csum_size = sizeof(struct ext4_dir_entry_tail);
  1554. bh2 = ext4_append(handle, dir, &newblock);
  1555. if (IS_ERR(bh2)) {
  1556. brelse(*bh);
  1557. *bh = NULL;
  1558. return (struct ext4_dir_entry_2 *) bh2;
  1559. }
  1560. BUFFER_TRACE(*bh, "get_write_access");
  1561. err = ext4_journal_get_write_access(handle, *bh);
  1562. if (err)
  1563. goto journal_error;
  1564. BUFFER_TRACE(frame->bh, "get_write_access");
  1565. err = ext4_journal_get_write_access(handle, frame->bh);
  1566. if (err)
  1567. goto journal_error;
  1568. data2 = bh2->b_data;
  1569. /* create map in the end of data2 block */
  1570. map = (struct dx_map_entry *) (data2 + blocksize);
  1571. count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
  1572. blocksize, hinfo, map);
  1573. map -= count;
  1574. dx_sort_map(map, count);
  1575. /* Split the existing block in the middle, size-wise */
  1576. size = 0;
  1577. move = 0;
  1578. for (i = count-1; i >= 0; i--) {
  1579. /* is more than half of this entry in 2nd half of the block? */
  1580. if (size + map[i].size/2 > blocksize/2)
  1581. break;
  1582. size += map[i].size;
  1583. move++;
  1584. }
  1585. /* map index at which we will split */
  1586. split = count - move;
  1587. hash2 = map[split].hash;
  1588. continued = hash2 == map[split - 1].hash;
  1589. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1590. (unsigned long)dx_get_block(frame->at),
  1591. hash2, split, count-split));
  1592. /* Fancy dance to stay within two buffers */
  1593. de2 = dx_move_dirents(data1, data2, map + split, count - split,
  1594. blocksize);
  1595. de = dx_pack_dirents(data1, blocksize);
  1596. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1597. (char *) de,
  1598. blocksize);
  1599. de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
  1600. (char *) de2,
  1601. blocksize);
  1602. if (csum_size) {
  1603. t = EXT4_DIRENT_TAIL(data2, blocksize);
  1604. initialize_dirent_tail(t, blocksize);
  1605. t = EXT4_DIRENT_TAIL(data1, blocksize);
  1606. initialize_dirent_tail(t, blocksize);
  1607. }
  1608. dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
  1609. blocksize, 1));
  1610. dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
  1611. blocksize, 1));
  1612. /* Which block gets the new entry? */
  1613. if (hinfo->hash >= hash2) {
  1614. swap(*bh, bh2);
  1615. de = de2;
  1616. }
  1617. dx_insert_block(frame, hash2 + continued, newblock);
  1618. err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
  1619. if (err)
  1620. goto journal_error;
  1621. err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1622. if (err)
  1623. goto journal_error;
  1624. brelse(bh2);
  1625. dxtrace(dx_show_index("frame", frame->entries));
  1626. return de;
  1627. journal_error:
  1628. brelse(*bh);
  1629. brelse(bh2);
  1630. *bh = NULL;
  1631. ext4_std_error(dir->i_sb, err);
  1632. return ERR_PTR(err);
  1633. }
  1634. int ext4_find_dest_de(struct inode *dir, struct inode *inode,
  1635. struct buffer_head *bh,
  1636. void *buf, int buf_size,
  1637. struct ext4_filename *fname,
  1638. struct ext4_dir_entry_2 **dest_de)
  1639. {
  1640. struct ext4_dir_entry_2 *de;
  1641. unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname));
  1642. int nlen, rlen;
  1643. unsigned int offset = 0;
  1644. char *top;
  1645. de = (struct ext4_dir_entry_2 *)buf;
  1646. top = buf + buf_size - reclen;
  1647. while ((char *) de <= top) {
  1648. if (ext4_check_dir_entry(dir, NULL, de, bh,
  1649. buf, buf_size, offset))
  1650. return -EFSCORRUPTED;
  1651. if (ext4_match(fname, de))
  1652. return -EEXIST;
  1653. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1654. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1655. if ((de->inode ? rlen - nlen : rlen) >= reclen)
  1656. break;
  1657. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1658. offset += rlen;
  1659. }
  1660. if ((char *) de > top)
  1661. return -ENOSPC;
  1662. *dest_de = de;
  1663. return 0;
  1664. }
  1665. void ext4_insert_dentry(struct inode *inode,
  1666. struct ext4_dir_entry_2 *de,
  1667. int buf_size,
  1668. struct ext4_filename *fname)
  1669. {
  1670. int nlen, rlen;
  1671. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1672. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1673. if (de->inode) {
  1674. struct ext4_dir_entry_2 *de1 =
  1675. (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1676. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
  1677. de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
  1678. de = de1;
  1679. }
  1680. de->file_type = EXT4_FT_UNKNOWN;
  1681. de->inode = cpu_to_le32(inode->i_ino);
  1682. ext4_set_de_type(inode->i_sb, de, inode->i_mode);
  1683. de->name_len = fname_len(fname);
  1684. memcpy(de->name, fname_name(fname), fname_len(fname));
  1685. }
  1686. /*
  1687. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1688. * it points to a directory entry which is guaranteed to be large
  1689. * enough for new directory entry. If de is NULL, then
  1690. * add_dirent_to_buf will attempt search the directory block for
  1691. * space. It will return -ENOSPC if no space is available, and -EIO
  1692. * and -EEXIST if directory entry already exists.
  1693. */
  1694. static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
  1695. struct inode *dir,
  1696. struct inode *inode, struct ext4_dir_entry_2 *de,
  1697. struct buffer_head *bh)
  1698. {
  1699. unsigned int blocksize = dir->i_sb->s_blocksize;
  1700. int csum_size = 0;
  1701. int err;
  1702. if (ext4_has_metadata_csum(inode->i_sb))
  1703. csum_size = sizeof(struct ext4_dir_entry_tail);
  1704. if (!de) {
  1705. err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
  1706. blocksize - csum_size, fname, &de);
  1707. if (err)
  1708. return err;
  1709. }
  1710. BUFFER_TRACE(bh, "get_write_access");
  1711. err = ext4_journal_get_write_access(handle, bh);
  1712. if (err) {
  1713. ext4_std_error(dir->i_sb, err);
  1714. return err;
  1715. }
  1716. /* By now the buffer is marked for journaling */
  1717. ext4_insert_dentry(inode, de, blocksize, fname);
  1718. /*
  1719. * XXX shouldn't update any times until successful
  1720. * completion of syscall, but too many callers depend
  1721. * on this.
  1722. *
  1723. * XXX similarly, too many callers depend on
  1724. * ext4_new_inode() setting the times, but error
  1725. * recovery deletes the inode, so the worst that can
  1726. * happen is that the times are slightly out of date
  1727. * and/or different from the directory change time.
  1728. */
  1729. dir->i_mtime = dir->i_ctime = current_time(dir);
  1730. ext4_update_dx_flag(dir);
  1731. inode_inc_iversion(dir);
  1732. ext4_mark_inode_dirty(handle, dir);
  1733. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1734. err = ext4_handle_dirty_dirent_node(handle, dir, bh);
  1735. if (err)
  1736. ext4_std_error(dir->i_sb, err);
  1737. return 0;
  1738. }
  1739. /*
  1740. * This converts a one block unindexed directory to a 3 block indexed
  1741. * directory, and adds the dentry to the indexed directory.
  1742. */
  1743. static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
  1744. struct inode *dir,
  1745. struct inode *inode, struct buffer_head *bh)
  1746. {
  1747. struct buffer_head *bh2;
  1748. struct dx_root *root;
  1749. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  1750. struct dx_entry *entries;
  1751. struct ext4_dir_entry_2 *de, *de2;
  1752. struct ext4_dir_entry_tail *t;
  1753. char *data1, *top;
  1754. unsigned len;
  1755. int retval;
  1756. unsigned blocksize;
  1757. ext4_lblk_t block;
  1758. struct fake_dirent *fde;
  1759. int csum_size = 0;
  1760. if (ext4_has_metadata_csum(inode->i_sb))
  1761. csum_size = sizeof(struct ext4_dir_entry_tail);
  1762. blocksize = dir->i_sb->s_blocksize;
  1763. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1764. BUFFER_TRACE(bh, "get_write_access");
  1765. retval = ext4_journal_get_write_access(handle, bh);
  1766. if (retval) {
  1767. ext4_std_error(dir->i_sb, retval);
  1768. brelse(bh);
  1769. return retval;
  1770. }
  1771. root = (struct dx_root *) bh->b_data;
  1772. /* The 0th block becomes the root, move the dirents out */
  1773. fde = &root->dotdot;
  1774. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1775. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  1776. if ((char *) de >= (((char *) root) + blocksize)) {
  1777. EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
  1778. brelse(bh);
  1779. return -EFSCORRUPTED;
  1780. }
  1781. len = ((char *) root) + (blocksize - csum_size) - (char *) de;
  1782. /* Allocate new block for the 0th block's dirents */
  1783. bh2 = ext4_append(handle, dir, &block);
  1784. if (IS_ERR(bh2)) {
  1785. brelse(bh);
  1786. return PTR_ERR(bh2);
  1787. }
  1788. ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
  1789. data1 = bh2->b_data;
  1790. memcpy (data1, de, len);
  1791. de = (struct ext4_dir_entry_2 *) data1;
  1792. top = data1 + len;
  1793. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
  1794. de = de2;
  1795. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1796. (char *) de,
  1797. blocksize);
  1798. if (csum_size) {
  1799. t = EXT4_DIRENT_TAIL(data1, blocksize);
  1800. initialize_dirent_tail(t, blocksize);
  1801. }
  1802. /* Initialize the root; the dot dirents already exist */
  1803. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1804. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
  1805. blocksize);
  1806. memset (&root->info, 0, sizeof(root->info));
  1807. root->info.info_length = sizeof(root->info);
  1808. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1809. entries = root->entries;
  1810. dx_set_block(entries, 1);
  1811. dx_set_count(entries, 1);
  1812. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1813. /* Initialize as for dx_probe */
  1814. fname->hinfo.hash_version = root->info.hash_version;
  1815. if (fname->hinfo.hash_version <= DX_HASH_TEA)
  1816. fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1817. fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1818. ext4fs_dirhash(fname_name(fname), fname_len(fname), &fname->hinfo);
  1819. memset(frames, 0, sizeof(frames));
  1820. frame = frames;
  1821. frame->entries = entries;
  1822. frame->at = entries;
  1823. frame->bh = bh;
  1824. retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1825. if (retval)
  1826. goto out_frames;
  1827. retval = ext4_handle_dirty_dirent_node(handle, dir, bh2);
  1828. if (retval)
  1829. goto out_frames;
  1830. de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
  1831. if (IS_ERR(de)) {
  1832. retval = PTR_ERR(de);
  1833. goto out_frames;
  1834. }
  1835. retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2);
  1836. out_frames:
  1837. /*
  1838. * Even if the block split failed, we have to properly write
  1839. * out all the changes we did so far. Otherwise we can end up
  1840. * with corrupted filesystem.
  1841. */
  1842. if (retval)
  1843. ext4_mark_inode_dirty(handle, dir);
  1844. dx_release(frames);
  1845. brelse(bh2);
  1846. return retval;
  1847. }
  1848. /*
  1849. * ext4_add_entry()
  1850. *
  1851. * adds a file entry to the specified directory, using the same
  1852. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1853. *
  1854. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1855. * may not sleep between calling this and putting something into
  1856. * the entry, as someone else might have used it while you slept.
  1857. */
  1858. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1859. struct inode *inode)
  1860. {
  1861. struct inode *dir = d_inode(dentry->d_parent);
  1862. struct buffer_head *bh = NULL;
  1863. struct ext4_dir_entry_2 *de;
  1864. struct ext4_dir_entry_tail *t;
  1865. struct super_block *sb;
  1866. struct ext4_filename fname;
  1867. int retval;
  1868. int dx_fallback=0;
  1869. unsigned blocksize;
  1870. ext4_lblk_t block, blocks;
  1871. int csum_size = 0;
  1872. if (ext4_has_metadata_csum(inode->i_sb))
  1873. csum_size = sizeof(struct ext4_dir_entry_tail);
  1874. sb = dir->i_sb;
  1875. blocksize = sb->s_blocksize;
  1876. if (!dentry->d_name.len)
  1877. return -EINVAL;
  1878. retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
  1879. if (retval)
  1880. return retval;
  1881. if (ext4_has_inline_data(dir)) {
  1882. retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
  1883. if (retval < 0)
  1884. goto out;
  1885. if (retval == 1) {
  1886. retval = 0;
  1887. goto out;
  1888. }
  1889. }
  1890. if (is_dx(dir)) {
  1891. retval = ext4_dx_add_entry(handle, &fname, dir, inode);
  1892. if (!retval || (retval != ERR_BAD_DX_DIR))
  1893. goto out;
  1894. /* Can we just ignore htree data? */
  1895. if (ext4_has_metadata_csum(sb)) {
  1896. EXT4_ERROR_INODE(dir,
  1897. "Directory has corrupted htree index.");
  1898. retval = -EFSCORRUPTED;
  1899. goto out;
  1900. }
  1901. ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
  1902. dx_fallback++;
  1903. ext4_mark_inode_dirty(handle, dir);
  1904. }
  1905. blocks = dir->i_size >> sb->s_blocksize_bits;
  1906. for (block = 0; block < blocks; block++) {
  1907. bh = ext4_read_dirblock(dir, block, DIRENT);
  1908. if (bh == NULL) {
  1909. bh = ext4_bread(handle, dir, block,
  1910. EXT4_GET_BLOCKS_CREATE);
  1911. goto add_to_new_block;
  1912. }
  1913. if (IS_ERR(bh)) {
  1914. retval = PTR_ERR(bh);
  1915. bh = NULL;
  1916. goto out;
  1917. }
  1918. retval = add_dirent_to_buf(handle, &fname, dir, inode,
  1919. NULL, bh);
  1920. if (retval != -ENOSPC)
  1921. goto out;
  1922. if (blocks == 1 && !dx_fallback &&
  1923. ext4_has_feature_dir_index(sb)) {
  1924. retval = make_indexed_dir(handle, &fname, dir,
  1925. inode, bh);
  1926. bh = NULL; /* make_indexed_dir releases bh */
  1927. goto out;
  1928. }
  1929. brelse(bh);
  1930. }
  1931. bh = ext4_append(handle, dir, &block);
  1932. add_to_new_block:
  1933. if (IS_ERR(bh)) {
  1934. retval = PTR_ERR(bh);
  1935. bh = NULL;
  1936. goto out;
  1937. }
  1938. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1939. de->inode = 0;
  1940. de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
  1941. if (csum_size) {
  1942. t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
  1943. initialize_dirent_tail(t, blocksize);
  1944. }
  1945. retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
  1946. out:
  1947. ext4_fname_free_filename(&fname);
  1948. brelse(bh);
  1949. if (retval == 0)
  1950. ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
  1951. return retval;
  1952. }
  1953. /*
  1954. * Returns 0 for success, or a negative error value
  1955. */
  1956. static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
  1957. struct inode *dir, struct inode *inode)
  1958. {
  1959. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  1960. struct dx_entry *entries, *at;
  1961. struct buffer_head *bh;
  1962. struct super_block *sb = dir->i_sb;
  1963. struct ext4_dir_entry_2 *de;
  1964. int restart;
  1965. int err;
  1966. again:
  1967. restart = 0;
  1968. frame = dx_probe(fname, dir, NULL, frames);
  1969. if (IS_ERR(frame))
  1970. return PTR_ERR(frame);
  1971. entries = frame->entries;
  1972. at = frame->at;
  1973. bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT_HTREE);
  1974. if (IS_ERR(bh)) {
  1975. err = PTR_ERR(bh);
  1976. bh = NULL;
  1977. goto cleanup;
  1978. }
  1979. BUFFER_TRACE(bh, "get_write_access");
  1980. err = ext4_journal_get_write_access(handle, bh);
  1981. if (err)
  1982. goto journal_error;
  1983. err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
  1984. if (err != -ENOSPC)
  1985. goto cleanup;
  1986. err = 0;
  1987. /* Block full, should compress but for now just split */
  1988. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1989. dx_get_count(entries), dx_get_limit(entries)));
  1990. /* Need to split index? */
  1991. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1992. ext4_lblk_t newblock;
  1993. int levels = frame - frames + 1;
  1994. unsigned int icount;
  1995. int add_level = 1;
  1996. struct dx_entry *entries2;
  1997. struct dx_node *node2;
  1998. struct buffer_head *bh2;
  1999. while (frame > frames) {
  2000. if (dx_get_count((frame - 1)->entries) <
  2001. dx_get_limit((frame - 1)->entries)) {
  2002. add_level = 0;
  2003. break;
  2004. }
  2005. frame--; /* split higher index block */
  2006. at = frame->at;
  2007. entries = frame->entries;
  2008. restart = 1;
  2009. }
  2010. if (add_level && levels == ext4_dir_htree_level(sb)) {
  2011. ext4_warning(sb, "Directory (ino: %lu) index full, "
  2012. "reach max htree level :%d",
  2013. dir->i_ino, levels);
  2014. if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
  2015. ext4_warning(sb, "Large directory feature is "
  2016. "not enabled on this "
  2017. "filesystem");
  2018. }
  2019. err = -ENOSPC;
  2020. goto cleanup;
  2021. }
  2022. icount = dx_get_count(entries);
  2023. bh2 = ext4_append(handle, dir, &newblock);
  2024. if (IS_ERR(bh2)) {
  2025. err = PTR_ERR(bh2);
  2026. goto cleanup;
  2027. }
  2028. node2 = (struct dx_node *)(bh2->b_data);
  2029. entries2 = node2->entries;
  2030. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  2031. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  2032. sb->s_blocksize);
  2033. BUFFER_TRACE(frame->bh, "get_write_access");
  2034. err = ext4_journal_get_write_access(handle, frame->bh);
  2035. if (err)
  2036. goto journal_error;
  2037. if (!add_level) {
  2038. unsigned icount1 = icount/2, icount2 = icount - icount1;
  2039. unsigned hash2 = dx_get_hash(entries + icount1);
  2040. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  2041. icount1, icount2));
  2042. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  2043. err = ext4_journal_get_write_access(handle,
  2044. (frame - 1)->bh);
  2045. if (err)
  2046. goto journal_error;
  2047. memcpy((char *) entries2, (char *) (entries + icount1),
  2048. icount2 * sizeof(struct dx_entry));
  2049. dx_set_count(entries, icount1);
  2050. dx_set_count(entries2, icount2);
  2051. dx_set_limit(entries2, dx_node_limit(dir));
  2052. /* Which index block gets the new entry? */
  2053. if (at - entries >= icount1) {
  2054. frame->at = at = at - entries - icount1 + entries2;
  2055. frame->entries = entries = entries2;
  2056. swap(frame->bh, bh2);
  2057. }
  2058. dx_insert_block((frame - 1), hash2, newblock);
  2059. dxtrace(dx_show_index("node", frame->entries));
  2060. dxtrace(dx_show_index("node",
  2061. ((struct dx_node *) bh2->b_data)->entries));
  2062. err = ext4_handle_dirty_dx_node(handle, dir, bh2);
  2063. if (err)
  2064. goto journal_error;
  2065. brelse (bh2);
  2066. err = ext4_handle_dirty_dx_node(handle, dir,
  2067. (frame - 1)->bh);
  2068. if (err)
  2069. goto journal_error;
  2070. if (restart) {
  2071. err = ext4_handle_dirty_dx_node(handle, dir,
  2072. frame->bh);
  2073. goto journal_error;
  2074. }
  2075. } else {
  2076. struct dx_root *dxroot;
  2077. memcpy((char *) entries2, (char *) entries,
  2078. icount * sizeof(struct dx_entry));
  2079. dx_set_limit(entries2, dx_node_limit(dir));
  2080. /* Set up root */
  2081. dx_set_count(entries, 1);
  2082. dx_set_block(entries + 0, newblock);
  2083. dxroot = (struct dx_root *)frames[0].bh->b_data;
  2084. dxroot->info.indirect_levels += 1;
  2085. dxtrace(printk(KERN_DEBUG
  2086. "Creating %d level index...\n",
  2087. dxroot->info.indirect_levels));
  2088. err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  2089. if (err)
  2090. goto journal_error;
  2091. err = ext4_handle_dirty_dx_node(handle, dir, bh2);
  2092. brelse(bh2);
  2093. restart = 1;
  2094. goto journal_error;
  2095. }
  2096. }
  2097. de = do_split(handle, dir, &bh, frame, &fname->hinfo);
  2098. if (IS_ERR(de)) {
  2099. err = PTR_ERR(de);
  2100. goto cleanup;
  2101. }
  2102. err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
  2103. goto cleanup;
  2104. journal_error:
  2105. ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
  2106. cleanup:
  2107. brelse(bh);
  2108. dx_release(frames);
  2109. /* @restart is true means htree-path has been changed, we need to
  2110. * repeat dx_probe() to find out valid htree-path
  2111. */
  2112. if (restart && err == 0)
  2113. goto again;
  2114. return err;
  2115. }
  2116. /*
  2117. * ext4_generic_delete_entry deletes a directory entry by merging it
  2118. * with the previous entry
  2119. */
  2120. int ext4_generic_delete_entry(handle_t *handle,
  2121. struct inode *dir,
  2122. struct ext4_dir_entry_2 *de_del,
  2123. struct buffer_head *bh,
  2124. void *entry_buf,
  2125. int buf_size,
  2126. int csum_size)
  2127. {
  2128. struct ext4_dir_entry_2 *de, *pde;
  2129. unsigned int blocksize = dir->i_sb->s_blocksize;
  2130. int i;
  2131. i = 0;
  2132. pde = NULL;
  2133. de = (struct ext4_dir_entry_2 *)entry_buf;
  2134. while (i < buf_size - csum_size) {
  2135. if (ext4_check_dir_entry(dir, NULL, de, bh,
  2136. bh->b_data, bh->b_size, i))
  2137. return -EFSCORRUPTED;
  2138. if (de == de_del) {
  2139. if (pde)
  2140. pde->rec_len = ext4_rec_len_to_disk(
  2141. ext4_rec_len_from_disk(pde->rec_len,
  2142. blocksize) +
  2143. ext4_rec_len_from_disk(de->rec_len,
  2144. blocksize),
  2145. blocksize);
  2146. else
  2147. de->inode = 0;
  2148. inode_inc_iversion(dir);
  2149. return 0;
  2150. }
  2151. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  2152. pde = de;
  2153. de = ext4_next_entry(de, blocksize);
  2154. }
  2155. return -ENOENT;
  2156. }
  2157. static int ext4_delete_entry(handle_t *handle,
  2158. struct inode *dir,
  2159. struct ext4_dir_entry_2 *de_del,
  2160. struct buffer_head *bh)
  2161. {
  2162. int err, csum_size = 0;
  2163. if (ext4_has_inline_data(dir)) {
  2164. int has_inline_data = 1;
  2165. err = ext4_delete_inline_entry(handle, dir, de_del, bh,
  2166. &has_inline_data);
  2167. if (has_inline_data)
  2168. return err;
  2169. }
  2170. if (ext4_has_metadata_csum(dir->i_sb))
  2171. csum_size = sizeof(struct ext4_dir_entry_tail);
  2172. BUFFER_TRACE(bh, "get_write_access");
  2173. err = ext4_journal_get_write_access(handle, bh);
  2174. if (unlikely(err))
  2175. goto out;
  2176. err = ext4_generic_delete_entry(handle, dir, de_del,
  2177. bh, bh->b_data,
  2178. dir->i_sb->s_blocksize, csum_size);
  2179. if (err)
  2180. goto out;
  2181. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  2182. err = ext4_handle_dirty_dirent_node(handle, dir, bh);
  2183. if (unlikely(err))
  2184. goto out;
  2185. return 0;
  2186. out:
  2187. if (err != -ENOENT)
  2188. ext4_std_error(dir->i_sb, err);
  2189. return err;
  2190. }
  2191. /*
  2192. * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
  2193. * since this indicates that nlinks count was previously 1 to avoid overflowing
  2194. * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
  2195. * that subdirectory link counts are not being maintained accurately.
  2196. *
  2197. * The caller has already checked for i_nlink overflow in case the DIR_LINK
  2198. * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
  2199. * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
  2200. * on regular files) and to avoid creating huge/slow non-HTREE directories.
  2201. */
  2202. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  2203. {
  2204. inc_nlink(inode);
  2205. if (is_dx(inode) &&
  2206. (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
  2207. set_nlink(inode, 1);
  2208. }
  2209. /*
  2210. * If a directory had nlink == 1, then we should let it be 1. This indicates
  2211. * directory has >EXT4_LINK_MAX subdirs.
  2212. */
  2213. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  2214. {
  2215. if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
  2216. drop_nlink(inode);
  2217. }
  2218. static int ext4_add_nondir(handle_t *handle,
  2219. struct dentry *dentry, struct inode *inode)
  2220. {
  2221. int err = ext4_add_entry(handle, dentry, inode);
  2222. if (!err) {
  2223. ext4_mark_inode_dirty(handle, inode);
  2224. d_instantiate_new(dentry, inode);
  2225. return 0;
  2226. }
  2227. drop_nlink(inode);
  2228. unlock_new_inode(inode);
  2229. iput(inode);
  2230. return err;
  2231. }
  2232. /*
  2233. * By the time this is called, we already have created
  2234. * the directory cache entry for the new file, but it
  2235. * is so far negative - it has no inode.
  2236. *
  2237. * If the create succeeds, we fill in the inode information
  2238. * with d_instantiate().
  2239. */
  2240. static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2241. bool excl)
  2242. {
  2243. handle_t *handle;
  2244. struct inode *inode;
  2245. int err, credits, retries = 0;
  2246. err = dquot_initialize(dir);
  2247. if (err)
  2248. return err;
  2249. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2250. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2251. retry:
  2252. inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
  2253. NULL, EXT4_HT_DIR, credits);
  2254. handle = ext4_journal_current_handle();
  2255. err = PTR_ERR(inode);
  2256. if (!IS_ERR(inode)) {
  2257. inode->i_op = &ext4_file_inode_operations;
  2258. inode->i_fop = &ext4_file_operations;
  2259. ext4_set_aops(inode);
  2260. err = ext4_add_nondir(handle, dentry, inode);
  2261. if (!err && IS_DIRSYNC(dir))
  2262. ext4_handle_sync(handle);
  2263. }
  2264. if (handle)
  2265. ext4_journal_stop(handle);
  2266. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2267. goto retry;
  2268. return err;
  2269. }
  2270. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  2271. umode_t mode, dev_t rdev)
  2272. {
  2273. handle_t *handle;
  2274. struct inode *inode;
  2275. int err, credits, retries = 0;
  2276. err = dquot_initialize(dir);
  2277. if (err)
  2278. return err;
  2279. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2280. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2281. retry:
  2282. inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
  2283. NULL, EXT4_HT_DIR, credits);
  2284. handle = ext4_journal_current_handle();
  2285. err = PTR_ERR(inode);
  2286. if (!IS_ERR(inode)) {
  2287. init_special_inode(inode, inode->i_mode, rdev);
  2288. inode->i_op = &ext4_special_inode_operations;
  2289. err = ext4_add_nondir(handle, dentry, inode);
  2290. if (!err && IS_DIRSYNC(dir))
  2291. ext4_handle_sync(handle);
  2292. }
  2293. if (handle)
  2294. ext4_journal_stop(handle);
  2295. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2296. goto retry;
  2297. return err;
  2298. }
  2299. static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  2300. {
  2301. handle_t *handle;
  2302. struct inode *inode;
  2303. int err, retries = 0;
  2304. err = dquot_initialize(dir);
  2305. if (err)
  2306. return err;
  2307. retry:
  2308. inode = ext4_new_inode_start_handle(dir, mode,
  2309. NULL, 0, NULL,
  2310. EXT4_HT_DIR,
  2311. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2312. 4 + EXT4_XATTR_TRANS_BLOCKS);
  2313. handle = ext4_journal_current_handle();
  2314. err = PTR_ERR(inode);
  2315. if (!IS_ERR(inode)) {
  2316. inode->i_op = &ext4_file_inode_operations;
  2317. inode->i_fop = &ext4_file_operations;
  2318. ext4_set_aops(inode);
  2319. d_tmpfile(dentry, inode);
  2320. err = ext4_orphan_add(handle, inode);
  2321. if (err)
  2322. goto err_unlock_inode;
  2323. mark_inode_dirty(inode);
  2324. unlock_new_inode(inode);
  2325. }
  2326. if (handle)
  2327. ext4_journal_stop(handle);
  2328. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2329. goto retry;
  2330. return err;
  2331. err_unlock_inode:
  2332. ext4_journal_stop(handle);
  2333. unlock_new_inode(inode);
  2334. return err;
  2335. }
  2336. struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
  2337. struct ext4_dir_entry_2 *de,
  2338. int blocksize, int csum_size,
  2339. unsigned int parent_ino, int dotdot_real_len)
  2340. {
  2341. de->inode = cpu_to_le32(inode->i_ino);
  2342. de->name_len = 1;
  2343. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
  2344. blocksize);
  2345. strcpy(de->name, ".");
  2346. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2347. de = ext4_next_entry(de, blocksize);
  2348. de->inode = cpu_to_le32(parent_ino);
  2349. de->name_len = 2;
  2350. if (!dotdot_real_len)
  2351. de->rec_len = ext4_rec_len_to_disk(blocksize -
  2352. (csum_size + EXT4_DIR_REC_LEN(1)),
  2353. blocksize);
  2354. else
  2355. de->rec_len = ext4_rec_len_to_disk(
  2356. EXT4_DIR_REC_LEN(de->name_len), blocksize);
  2357. strcpy(de->name, "..");
  2358. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2359. return ext4_next_entry(de, blocksize);
  2360. }
  2361. static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
  2362. struct inode *inode)
  2363. {
  2364. struct buffer_head *dir_block = NULL;
  2365. struct ext4_dir_entry_2 *de;
  2366. struct ext4_dir_entry_tail *t;
  2367. ext4_lblk_t block = 0;
  2368. unsigned int blocksize = dir->i_sb->s_blocksize;
  2369. int csum_size = 0;
  2370. int err;
  2371. if (ext4_has_metadata_csum(dir->i_sb))
  2372. csum_size = sizeof(struct ext4_dir_entry_tail);
  2373. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2374. err = ext4_try_create_inline_dir(handle, dir, inode);
  2375. if (err < 0 && err != -ENOSPC)
  2376. goto out;
  2377. if (!err)
  2378. goto out;
  2379. }
  2380. inode->i_size = 0;
  2381. dir_block = ext4_append(handle, inode, &block);
  2382. if (IS_ERR(dir_block))
  2383. return PTR_ERR(dir_block);
  2384. de = (struct ext4_dir_entry_2 *)dir_block->b_data;
  2385. ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
  2386. set_nlink(inode, 2);
  2387. if (csum_size) {
  2388. t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
  2389. initialize_dirent_tail(t, blocksize);
  2390. }
  2391. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  2392. err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
  2393. if (err)
  2394. goto out;
  2395. set_buffer_verified(dir_block);
  2396. out:
  2397. brelse(dir_block);
  2398. return err;
  2399. }
  2400. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2401. {
  2402. handle_t *handle;
  2403. struct inode *inode;
  2404. int err, credits, retries = 0;
  2405. if (EXT4_DIR_LINK_MAX(dir))
  2406. return -EMLINK;
  2407. err = dquot_initialize(dir);
  2408. if (err)
  2409. return err;
  2410. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2411. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2412. retry:
  2413. inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
  2414. &dentry->d_name,
  2415. 0, NULL, EXT4_HT_DIR, credits);
  2416. handle = ext4_journal_current_handle();
  2417. err = PTR_ERR(inode);
  2418. if (IS_ERR(inode))
  2419. goto out_stop;
  2420. inode->i_op = &ext4_dir_inode_operations;
  2421. inode->i_fop = &ext4_dir_operations;
  2422. err = ext4_init_new_dir(handle, dir, inode);
  2423. if (err)
  2424. goto out_clear_inode;
  2425. err = ext4_mark_inode_dirty(handle, inode);
  2426. if (!err)
  2427. err = ext4_add_entry(handle, dentry, inode);
  2428. if (err) {
  2429. out_clear_inode:
  2430. clear_nlink(inode);
  2431. unlock_new_inode(inode);
  2432. ext4_mark_inode_dirty(handle, inode);
  2433. iput(inode);
  2434. goto out_stop;
  2435. }
  2436. ext4_inc_count(handle, dir);
  2437. ext4_update_dx_flag(dir);
  2438. err = ext4_mark_inode_dirty(handle, dir);
  2439. if (err)
  2440. goto out_clear_inode;
  2441. d_instantiate_new(dentry, inode);
  2442. if (IS_DIRSYNC(dir))
  2443. ext4_handle_sync(handle);
  2444. out_stop:
  2445. if (handle)
  2446. ext4_journal_stop(handle);
  2447. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2448. goto retry;
  2449. return err;
  2450. }
  2451. /*
  2452. * routine to check that the specified directory is empty (for rmdir)
  2453. */
  2454. bool ext4_empty_dir(struct inode *inode)
  2455. {
  2456. unsigned int offset;
  2457. struct buffer_head *bh;
  2458. struct ext4_dir_entry_2 *de;
  2459. struct super_block *sb;
  2460. if (ext4_has_inline_data(inode)) {
  2461. int has_inline_data = 1;
  2462. int ret;
  2463. ret = empty_inline_dir(inode, &has_inline_data);
  2464. if (has_inline_data)
  2465. return ret;
  2466. }
  2467. sb = inode->i_sb;
  2468. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
  2469. EXT4_ERROR_INODE(inode, "invalid size");
  2470. return true;
  2471. }
  2472. /* The first directory block must not be a hole,
  2473. * so treat it as DIRENT_HTREE
  2474. */
  2475. bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
  2476. if (IS_ERR(bh))
  2477. return true;
  2478. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2479. if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
  2480. 0) ||
  2481. le32_to_cpu(de->inode) != inode->i_ino || strcmp(".", de->name)) {
  2482. ext4_warning_inode(inode, "directory missing '.'");
  2483. brelse(bh);
  2484. return true;
  2485. }
  2486. offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  2487. de = ext4_next_entry(de, sb->s_blocksize);
  2488. if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
  2489. offset) ||
  2490. le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
  2491. ext4_warning_inode(inode, "directory missing '..'");
  2492. brelse(bh);
  2493. return true;
  2494. }
  2495. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  2496. while (offset < inode->i_size) {
  2497. if (!(offset & (sb->s_blocksize - 1))) {
  2498. unsigned int lblock;
  2499. brelse(bh);
  2500. lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  2501. bh = ext4_read_dirblock(inode, lblock, EITHER);
  2502. if (bh == NULL) {
  2503. offset += sb->s_blocksize;
  2504. continue;
  2505. }
  2506. if (IS_ERR(bh))
  2507. return true;
  2508. }
  2509. de = (struct ext4_dir_entry_2 *) (bh->b_data +
  2510. (offset & (sb->s_blocksize - 1)));
  2511. if (ext4_check_dir_entry(inode, NULL, de, bh,
  2512. bh->b_data, bh->b_size, offset)) {
  2513. offset = (offset | (sb->s_blocksize - 1)) + 1;
  2514. continue;
  2515. }
  2516. if (le32_to_cpu(de->inode)) {
  2517. brelse(bh);
  2518. return false;
  2519. }
  2520. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  2521. }
  2522. brelse(bh);
  2523. return true;
  2524. }
  2525. /*
  2526. * ext4_orphan_add() links an unlinked or truncated inode into a list of
  2527. * such inodes, starting at the superblock, in case we crash before the
  2528. * file is closed/deleted, or in case the inode truncate spans multiple
  2529. * transactions and the last transaction is not recovered after a crash.
  2530. *
  2531. * At filesystem recovery time, we walk this list deleting unlinked
  2532. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  2533. *
  2534. * Orphan list manipulation functions must be called under i_mutex unless
  2535. * we are just creating the inode or deleting it.
  2536. */
  2537. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  2538. {
  2539. struct super_block *sb = inode->i_sb;
  2540. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2541. struct ext4_iloc iloc;
  2542. int err = 0, rc;
  2543. bool dirty = false;
  2544. if (!sbi->s_journal || is_bad_inode(inode))
  2545. return 0;
  2546. WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
  2547. !inode_is_locked(inode));
  2548. /*
  2549. * Exit early if inode already is on orphan list. This is a big speedup
  2550. * since we don't have to contend on the global s_orphan_lock.
  2551. */
  2552. if (!list_empty(&EXT4_I(inode)->i_orphan))
  2553. return 0;
  2554. /*
  2555. * Orphan handling is only valid for files with data blocks
  2556. * being truncated, or files being unlinked. Note that we either
  2557. * hold i_mutex, or the inode can not be referenced from outside,
  2558. * so i_nlink should not be bumped due to race
  2559. */
  2560. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  2561. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  2562. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  2563. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  2564. if (err)
  2565. goto out;
  2566. err = ext4_reserve_inode_write(handle, inode, &iloc);
  2567. if (err)
  2568. goto out;
  2569. mutex_lock(&sbi->s_orphan_lock);
  2570. /*
  2571. * Due to previous errors inode may be already a part of on-disk
  2572. * orphan list. If so skip on-disk list modification.
  2573. */
  2574. if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
  2575. (le32_to_cpu(sbi->s_es->s_inodes_count))) {
  2576. /* Insert this inode at the head of the on-disk orphan list */
  2577. NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
  2578. sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  2579. dirty = true;
  2580. }
  2581. list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
  2582. mutex_unlock(&sbi->s_orphan_lock);
  2583. if (dirty) {
  2584. err = ext4_handle_dirty_super(handle, sb);
  2585. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  2586. if (!err)
  2587. err = rc;
  2588. if (err) {
  2589. /*
  2590. * We have to remove inode from in-memory list if
  2591. * addition to on disk orphan list failed. Stray orphan
  2592. * list entries can cause panics at unmount time.
  2593. */
  2594. mutex_lock(&sbi->s_orphan_lock);
  2595. list_del_init(&EXT4_I(inode)->i_orphan);
  2596. mutex_unlock(&sbi->s_orphan_lock);
  2597. }
  2598. } else
  2599. brelse(iloc.bh);
  2600. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  2601. jbd_debug(4, "orphan inode %lu will point to %d\n",
  2602. inode->i_ino, NEXT_ORPHAN(inode));
  2603. out:
  2604. ext4_std_error(sb, err);
  2605. return err;
  2606. }
  2607. /*
  2608. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  2609. * of such inodes stored on disk, because it is finally being cleaned up.
  2610. */
  2611. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  2612. {
  2613. struct list_head *prev;
  2614. struct ext4_inode_info *ei = EXT4_I(inode);
  2615. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2616. __u32 ino_next;
  2617. struct ext4_iloc iloc;
  2618. int err = 0;
  2619. if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
  2620. return 0;
  2621. WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
  2622. !inode_is_locked(inode));
  2623. /* Do this quick check before taking global s_orphan_lock. */
  2624. if (list_empty(&ei->i_orphan))
  2625. return 0;
  2626. if (handle) {
  2627. /* Grab inode buffer early before taking global s_orphan_lock */
  2628. err = ext4_reserve_inode_write(handle, inode, &iloc);
  2629. }
  2630. mutex_lock(&sbi->s_orphan_lock);
  2631. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  2632. prev = ei->i_orphan.prev;
  2633. list_del_init(&ei->i_orphan);
  2634. /* If we're on an error path, we may not have a valid
  2635. * transaction handle with which to update the orphan list on
  2636. * disk, but we still need to remove the inode from the linked
  2637. * list in memory. */
  2638. if (!handle || err) {
  2639. mutex_unlock(&sbi->s_orphan_lock);
  2640. goto out_err;
  2641. }
  2642. ino_next = NEXT_ORPHAN(inode);
  2643. if (prev == &sbi->s_orphan) {
  2644. jbd_debug(4, "superblock will point to %u\n", ino_next);
  2645. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  2646. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  2647. if (err) {
  2648. mutex_unlock(&sbi->s_orphan_lock);
  2649. goto out_brelse;
  2650. }
  2651. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  2652. mutex_unlock(&sbi->s_orphan_lock);
  2653. err = ext4_handle_dirty_super(handle, inode->i_sb);
  2654. } else {
  2655. struct ext4_iloc iloc2;
  2656. struct inode *i_prev =
  2657. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  2658. jbd_debug(4, "orphan inode %lu will point to %u\n",
  2659. i_prev->i_ino, ino_next);
  2660. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  2661. if (err) {
  2662. mutex_unlock(&sbi->s_orphan_lock);
  2663. goto out_brelse;
  2664. }
  2665. NEXT_ORPHAN(i_prev) = ino_next;
  2666. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  2667. mutex_unlock(&sbi->s_orphan_lock);
  2668. }
  2669. if (err)
  2670. goto out_brelse;
  2671. NEXT_ORPHAN(inode) = 0;
  2672. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  2673. out_err:
  2674. ext4_std_error(inode->i_sb, err);
  2675. return err;
  2676. out_brelse:
  2677. brelse(iloc.bh);
  2678. goto out_err;
  2679. }
  2680. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  2681. {
  2682. int retval;
  2683. struct inode *inode;
  2684. struct buffer_head *bh;
  2685. struct ext4_dir_entry_2 *de;
  2686. handle_t *handle = NULL;
  2687. if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
  2688. return -EIO;
  2689. /* Initialize quotas before so that eventual writes go in
  2690. * separate transaction */
  2691. retval = dquot_initialize(dir);
  2692. if (retval)
  2693. return retval;
  2694. retval = dquot_initialize(d_inode(dentry));
  2695. if (retval)
  2696. return retval;
  2697. retval = -ENOENT;
  2698. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2699. if (IS_ERR(bh))
  2700. return PTR_ERR(bh);
  2701. if (!bh)
  2702. goto end_rmdir;
  2703. inode = d_inode(dentry);
  2704. retval = -EFSCORRUPTED;
  2705. if (le32_to_cpu(de->inode) != inode->i_ino)
  2706. goto end_rmdir;
  2707. retval = -ENOTEMPTY;
  2708. if (!ext4_empty_dir(inode))
  2709. goto end_rmdir;
  2710. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2711. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2712. if (IS_ERR(handle)) {
  2713. retval = PTR_ERR(handle);
  2714. handle = NULL;
  2715. goto end_rmdir;
  2716. }
  2717. if (IS_DIRSYNC(dir))
  2718. ext4_handle_sync(handle);
  2719. retval = ext4_delete_entry(handle, dir, de, bh);
  2720. if (retval)
  2721. goto end_rmdir;
  2722. if (!EXT4_DIR_LINK_EMPTY(inode))
  2723. ext4_warning_inode(inode,
  2724. "empty directory '%.*s' has too many links (%u)",
  2725. dentry->d_name.len, dentry->d_name.name,
  2726. inode->i_nlink);
  2727. inode_inc_iversion(inode);
  2728. clear_nlink(inode);
  2729. /* There's no need to set i_disksize: the fact that i_nlink is
  2730. * zero will ensure that the right thing happens during any
  2731. * recovery. */
  2732. inode->i_size = 0;
  2733. ext4_orphan_add(handle, inode);
  2734. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
  2735. ext4_mark_inode_dirty(handle, inode);
  2736. ext4_dec_count(handle, dir);
  2737. ext4_update_dx_flag(dir);
  2738. ext4_mark_inode_dirty(handle, dir);
  2739. end_rmdir:
  2740. brelse(bh);
  2741. if (handle)
  2742. ext4_journal_stop(handle);
  2743. return retval;
  2744. }
  2745. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  2746. {
  2747. int retval;
  2748. struct inode *inode;
  2749. struct buffer_head *bh;
  2750. struct ext4_dir_entry_2 *de;
  2751. handle_t *handle = NULL;
  2752. if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
  2753. return -EIO;
  2754. trace_ext4_unlink_enter(dir, dentry);
  2755. /* Initialize quotas before so that eventual writes go
  2756. * in separate transaction */
  2757. retval = dquot_initialize(dir);
  2758. if (retval)
  2759. return retval;
  2760. retval = dquot_initialize(d_inode(dentry));
  2761. if (retval)
  2762. return retval;
  2763. retval = -ENOENT;
  2764. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2765. if (IS_ERR(bh))
  2766. return PTR_ERR(bh);
  2767. if (!bh)
  2768. goto end_unlink;
  2769. inode = d_inode(dentry);
  2770. retval = -EFSCORRUPTED;
  2771. if (le32_to_cpu(de->inode) != inode->i_ino)
  2772. goto end_unlink;
  2773. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2774. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2775. if (IS_ERR(handle)) {
  2776. retval = PTR_ERR(handle);
  2777. handle = NULL;
  2778. goto end_unlink;
  2779. }
  2780. if (IS_DIRSYNC(dir))
  2781. ext4_handle_sync(handle);
  2782. retval = ext4_delete_entry(handle, dir, de, bh);
  2783. if (retval)
  2784. goto end_unlink;
  2785. dir->i_ctime = dir->i_mtime = current_time(dir);
  2786. ext4_update_dx_flag(dir);
  2787. ext4_mark_inode_dirty(handle, dir);
  2788. if (inode->i_nlink == 0)
  2789. ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
  2790. dentry->d_name.len, dentry->d_name.name);
  2791. else
  2792. drop_nlink(inode);
  2793. if (!inode->i_nlink)
  2794. ext4_orphan_add(handle, inode);
  2795. inode->i_ctime = current_time(inode);
  2796. ext4_mark_inode_dirty(handle, inode);
  2797. end_unlink:
  2798. brelse(bh);
  2799. if (handle)
  2800. ext4_journal_stop(handle);
  2801. trace_ext4_unlink_exit(dentry, retval);
  2802. return retval;
  2803. }
  2804. static int ext4_symlink(struct inode *dir,
  2805. struct dentry *dentry, const char *symname)
  2806. {
  2807. handle_t *handle;
  2808. struct inode *inode;
  2809. int err, len = strlen(symname);
  2810. int credits;
  2811. struct fscrypt_str disk_link;
  2812. if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
  2813. return -EIO;
  2814. err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
  2815. &disk_link);
  2816. if (err)
  2817. return err;
  2818. err = dquot_initialize(dir);
  2819. if (err)
  2820. return err;
  2821. if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
  2822. /*
  2823. * For non-fast symlinks, we just allocate inode and put it on
  2824. * orphan list in the first transaction => we need bitmap,
  2825. * group descriptor, sb, inode block, quota blocks, and
  2826. * possibly selinux xattr blocks.
  2827. */
  2828. credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2829. EXT4_XATTR_TRANS_BLOCKS;
  2830. } else {
  2831. /*
  2832. * Fast symlink. We have to add entry to directory
  2833. * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
  2834. * allocate new inode (bitmap, group descriptor, inode block,
  2835. * quota blocks, sb is already counted in previous macros).
  2836. */
  2837. credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2838. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
  2839. }
  2840. inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
  2841. &dentry->d_name, 0, NULL,
  2842. EXT4_HT_DIR, credits);
  2843. handle = ext4_journal_current_handle();
  2844. if (IS_ERR(inode)) {
  2845. if (handle)
  2846. ext4_journal_stop(handle);
  2847. return PTR_ERR(inode);
  2848. }
  2849. if (IS_ENCRYPTED(inode)) {
  2850. err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
  2851. if (err)
  2852. goto err_drop_inode;
  2853. inode->i_op = &ext4_encrypted_symlink_inode_operations;
  2854. }
  2855. if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
  2856. if (!IS_ENCRYPTED(inode))
  2857. inode->i_op = &ext4_symlink_inode_operations;
  2858. inode_nohighmem(inode);
  2859. ext4_set_aops(inode);
  2860. /*
  2861. * We cannot call page_symlink() with transaction started
  2862. * because it calls into ext4_write_begin() which can wait
  2863. * for transaction commit if we are running out of space
  2864. * and thus we deadlock. So we have to stop transaction now
  2865. * and restart it when symlink contents is written.
  2866. *
  2867. * To keep fs consistent in case of crash, we have to put inode
  2868. * to orphan list in the mean time.
  2869. */
  2870. drop_nlink(inode);
  2871. err = ext4_orphan_add(handle, inode);
  2872. ext4_journal_stop(handle);
  2873. handle = NULL;
  2874. if (err)
  2875. goto err_drop_inode;
  2876. err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
  2877. if (err)
  2878. goto err_drop_inode;
  2879. /*
  2880. * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
  2881. * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
  2882. */
  2883. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2884. EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2885. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
  2886. if (IS_ERR(handle)) {
  2887. err = PTR_ERR(handle);
  2888. handle = NULL;
  2889. goto err_drop_inode;
  2890. }
  2891. set_nlink(inode, 1);
  2892. err = ext4_orphan_del(handle, inode);
  2893. if (err)
  2894. goto err_drop_inode;
  2895. } else {
  2896. /* clear the extent format for fast symlink */
  2897. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  2898. if (!IS_ENCRYPTED(inode)) {
  2899. inode->i_op = &ext4_fast_symlink_inode_operations;
  2900. inode->i_link = (char *)&EXT4_I(inode)->i_data;
  2901. }
  2902. memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
  2903. disk_link.len);
  2904. inode->i_size = disk_link.len - 1;
  2905. }
  2906. EXT4_I(inode)->i_disksize = inode->i_size;
  2907. err = ext4_add_nondir(handle, dentry, inode);
  2908. if (!err && IS_DIRSYNC(dir))
  2909. ext4_handle_sync(handle);
  2910. if (handle)
  2911. ext4_journal_stop(handle);
  2912. goto out_free_encrypted_link;
  2913. err_drop_inode:
  2914. if (handle)
  2915. ext4_journal_stop(handle);
  2916. clear_nlink(inode);
  2917. unlock_new_inode(inode);
  2918. iput(inode);
  2919. out_free_encrypted_link:
  2920. if (disk_link.name != (unsigned char *)symname)
  2921. kfree(disk_link.name);
  2922. return err;
  2923. }
  2924. static int ext4_link(struct dentry *old_dentry,
  2925. struct inode *dir, struct dentry *dentry)
  2926. {
  2927. handle_t *handle;
  2928. struct inode *inode = d_inode(old_dentry);
  2929. int err, retries = 0;
  2930. if (inode->i_nlink >= EXT4_LINK_MAX)
  2931. return -EMLINK;
  2932. err = fscrypt_prepare_link(old_dentry, dir, dentry);
  2933. if (err)
  2934. return err;
  2935. if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
  2936. (!projid_eq(EXT4_I(dir)->i_projid,
  2937. EXT4_I(old_dentry->d_inode)->i_projid)))
  2938. return -EXDEV;
  2939. err = dquot_initialize(dir);
  2940. if (err)
  2941. return err;
  2942. retry:
  2943. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2944. (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2945. EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
  2946. if (IS_ERR(handle))
  2947. return PTR_ERR(handle);
  2948. if (IS_DIRSYNC(dir))
  2949. ext4_handle_sync(handle);
  2950. inode->i_ctime = current_time(inode);
  2951. ext4_inc_count(handle, inode);
  2952. ihold(inode);
  2953. err = ext4_add_entry(handle, dentry, inode);
  2954. if (!err) {
  2955. ext4_mark_inode_dirty(handle, inode);
  2956. /* this can happen only for tmpfile being
  2957. * linked the first time
  2958. */
  2959. if (inode->i_nlink == 1)
  2960. ext4_orphan_del(handle, inode);
  2961. d_instantiate(dentry, inode);
  2962. } else {
  2963. drop_nlink(inode);
  2964. iput(inode);
  2965. }
  2966. ext4_journal_stop(handle);
  2967. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2968. goto retry;
  2969. return err;
  2970. }
  2971. /*
  2972. * Try to find buffer head where contains the parent block.
  2973. * It should be the inode block if it is inlined or the 1st block
  2974. * if it is a normal dir.
  2975. */
  2976. static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
  2977. struct inode *inode,
  2978. int *retval,
  2979. struct ext4_dir_entry_2 **parent_de,
  2980. int *inlined)
  2981. {
  2982. struct buffer_head *bh;
  2983. if (!ext4_has_inline_data(inode)) {
  2984. /* The first directory block must not be a hole, so
  2985. * treat it as DIRENT_HTREE
  2986. */
  2987. bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
  2988. if (IS_ERR(bh)) {
  2989. *retval = PTR_ERR(bh);
  2990. return NULL;
  2991. }
  2992. *parent_de = ext4_next_entry(
  2993. (struct ext4_dir_entry_2 *)bh->b_data,
  2994. inode->i_sb->s_blocksize);
  2995. return bh;
  2996. }
  2997. *inlined = 1;
  2998. return ext4_get_first_inline_block(inode, parent_de, retval);
  2999. }
  3000. struct ext4_renament {
  3001. struct inode *dir;
  3002. struct dentry *dentry;
  3003. struct inode *inode;
  3004. bool is_dir;
  3005. int dir_nlink_delta;
  3006. /* entry for "dentry" */
  3007. struct buffer_head *bh;
  3008. struct ext4_dir_entry_2 *de;
  3009. int inlined;
  3010. /* entry for ".." in inode if it's a directory */
  3011. struct buffer_head *dir_bh;
  3012. struct ext4_dir_entry_2 *parent_de;
  3013. int dir_inlined;
  3014. };
  3015. static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
  3016. {
  3017. int retval;
  3018. ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
  3019. &retval, &ent->parent_de,
  3020. &ent->dir_inlined);
  3021. if (!ent->dir_bh)
  3022. return retval;
  3023. if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
  3024. return -EFSCORRUPTED;
  3025. BUFFER_TRACE(ent->dir_bh, "get_write_access");
  3026. return ext4_journal_get_write_access(handle, ent->dir_bh);
  3027. }
  3028. static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
  3029. unsigned dir_ino)
  3030. {
  3031. int retval;
  3032. ent->parent_de->inode = cpu_to_le32(dir_ino);
  3033. BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
  3034. if (!ent->dir_inlined) {
  3035. if (is_dx(ent->inode)) {
  3036. retval = ext4_handle_dirty_dx_node(handle,
  3037. ent->inode,
  3038. ent->dir_bh);
  3039. } else {
  3040. retval = ext4_handle_dirty_dirent_node(handle,
  3041. ent->inode,
  3042. ent->dir_bh);
  3043. }
  3044. } else {
  3045. retval = ext4_mark_inode_dirty(handle, ent->inode);
  3046. }
  3047. if (retval) {
  3048. ext4_std_error(ent->dir->i_sb, retval);
  3049. return retval;
  3050. }
  3051. return 0;
  3052. }
  3053. static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
  3054. unsigned ino, unsigned file_type)
  3055. {
  3056. int retval;
  3057. BUFFER_TRACE(ent->bh, "get write access");
  3058. retval = ext4_journal_get_write_access(handle, ent->bh);
  3059. if (retval)
  3060. return retval;
  3061. ent->de->inode = cpu_to_le32(ino);
  3062. if (ext4_has_feature_filetype(ent->dir->i_sb))
  3063. ent->de->file_type = file_type;
  3064. inode_inc_iversion(ent->dir);
  3065. ent->dir->i_ctime = ent->dir->i_mtime =
  3066. current_time(ent->dir);
  3067. ext4_mark_inode_dirty(handle, ent->dir);
  3068. BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
  3069. if (!ent->inlined) {
  3070. retval = ext4_handle_dirty_dirent_node(handle,
  3071. ent->dir, ent->bh);
  3072. if (unlikely(retval)) {
  3073. ext4_std_error(ent->dir->i_sb, retval);
  3074. return retval;
  3075. }
  3076. }
  3077. brelse(ent->bh);
  3078. ent->bh = NULL;
  3079. return 0;
  3080. }
  3081. static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
  3082. const struct qstr *d_name)
  3083. {
  3084. int retval = -ENOENT;
  3085. struct buffer_head *bh;
  3086. struct ext4_dir_entry_2 *de;
  3087. bh = ext4_find_entry(dir, d_name, &de, NULL);
  3088. if (IS_ERR(bh))
  3089. return PTR_ERR(bh);
  3090. if (bh) {
  3091. retval = ext4_delete_entry(handle, dir, de, bh);
  3092. brelse(bh);
  3093. }
  3094. return retval;
  3095. }
  3096. static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
  3097. int force_reread)
  3098. {
  3099. int retval;
  3100. /*
  3101. * ent->de could have moved from under us during htree split, so make
  3102. * sure that we are deleting the right entry. We might also be pointing
  3103. * to a stale entry in the unused part of ent->bh so just checking inum
  3104. * and the name isn't enough.
  3105. */
  3106. if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
  3107. ent->de->name_len != ent->dentry->d_name.len ||
  3108. strncmp(ent->de->name, ent->dentry->d_name.name,
  3109. ent->de->name_len) ||
  3110. force_reread) {
  3111. retval = ext4_find_delete_entry(handle, ent->dir,
  3112. &ent->dentry->d_name);
  3113. } else {
  3114. retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
  3115. if (retval == -ENOENT) {
  3116. retval = ext4_find_delete_entry(handle, ent->dir,
  3117. &ent->dentry->d_name);
  3118. }
  3119. }
  3120. if (retval) {
  3121. ext4_warning_inode(ent->dir,
  3122. "Deleting old file: nlink %d, error=%d",
  3123. ent->dir->i_nlink, retval);
  3124. }
  3125. }
  3126. static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
  3127. {
  3128. if (ent->dir_nlink_delta) {
  3129. if (ent->dir_nlink_delta == -1)
  3130. ext4_dec_count(handle, ent->dir);
  3131. else
  3132. ext4_inc_count(handle, ent->dir);
  3133. ext4_mark_inode_dirty(handle, ent->dir);
  3134. }
  3135. }
  3136. static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
  3137. int credits, handle_t **h)
  3138. {
  3139. struct inode *wh;
  3140. handle_t *handle;
  3141. int retries = 0;
  3142. /*
  3143. * for inode block, sb block, group summaries,
  3144. * and inode bitmap
  3145. */
  3146. credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
  3147. EXT4_XATTR_TRANS_BLOCKS + 4);
  3148. retry:
  3149. wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
  3150. &ent->dentry->d_name, 0, NULL,
  3151. EXT4_HT_DIR, credits);
  3152. handle = ext4_journal_current_handle();
  3153. if (IS_ERR(wh)) {
  3154. if (handle)
  3155. ext4_journal_stop(handle);
  3156. if (PTR_ERR(wh) == -ENOSPC &&
  3157. ext4_should_retry_alloc(ent->dir->i_sb, &retries))
  3158. goto retry;
  3159. } else {
  3160. *h = handle;
  3161. init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
  3162. wh->i_op = &ext4_special_inode_operations;
  3163. }
  3164. return wh;
  3165. }
  3166. /*
  3167. * Anybody can rename anything with this: the permission checks are left to the
  3168. * higher-level routines.
  3169. *
  3170. * n.b. old_{dentry,inode) refers to the source dentry/inode
  3171. * while new_{dentry,inode) refers to the destination dentry/inode
  3172. * This comes from rename(const char *oldpath, const char *newpath)
  3173. */
  3174. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  3175. struct inode *new_dir, struct dentry *new_dentry,
  3176. unsigned int flags)
  3177. {
  3178. handle_t *handle = NULL;
  3179. struct ext4_renament old = {
  3180. .dir = old_dir,
  3181. .dentry = old_dentry,
  3182. .inode = d_inode(old_dentry),
  3183. };
  3184. struct ext4_renament new = {
  3185. .dir = new_dir,
  3186. .dentry = new_dentry,
  3187. .inode = d_inode(new_dentry),
  3188. };
  3189. int force_reread;
  3190. int retval;
  3191. struct inode *whiteout = NULL;
  3192. int credits;
  3193. u8 old_file_type;
  3194. if (new.inode && new.inode->i_nlink == 0) {
  3195. EXT4_ERROR_INODE(new.inode,
  3196. "target of rename is already freed");
  3197. return -EFSCORRUPTED;
  3198. }
  3199. if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
  3200. (!projid_eq(EXT4_I(new_dir)->i_projid,
  3201. EXT4_I(old_dentry->d_inode)->i_projid)))
  3202. return -EXDEV;
  3203. retval = dquot_initialize(old.dir);
  3204. if (retval)
  3205. return retval;
  3206. retval = dquot_initialize(new.dir);
  3207. if (retval)
  3208. return retval;
  3209. /* Initialize quotas before so that eventual writes go
  3210. * in separate transaction */
  3211. if (new.inode) {
  3212. retval = dquot_initialize(new.inode);
  3213. if (retval)
  3214. return retval;
  3215. }
  3216. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
  3217. if (IS_ERR(old.bh))
  3218. return PTR_ERR(old.bh);
  3219. /*
  3220. * Check for inode number is _not_ due to possible IO errors.
  3221. * We might rmdir the source, keep it as pwd of some process
  3222. * and merrily kill the link to whatever was created under the
  3223. * same name. Goodbye sticky bit ;-<
  3224. */
  3225. retval = -ENOENT;
  3226. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  3227. goto end_rename;
  3228. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  3229. &new.de, &new.inlined);
  3230. if (IS_ERR(new.bh)) {
  3231. retval = PTR_ERR(new.bh);
  3232. new.bh = NULL;
  3233. goto end_rename;
  3234. }
  3235. if (new.bh) {
  3236. if (!new.inode) {
  3237. brelse(new.bh);
  3238. new.bh = NULL;
  3239. }
  3240. }
  3241. if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
  3242. ext4_alloc_da_blocks(old.inode);
  3243. credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3244. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  3245. if (!(flags & RENAME_WHITEOUT)) {
  3246. handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
  3247. if (IS_ERR(handle)) {
  3248. retval = PTR_ERR(handle);
  3249. handle = NULL;
  3250. goto end_rename;
  3251. }
  3252. } else {
  3253. whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
  3254. if (IS_ERR(whiteout)) {
  3255. retval = PTR_ERR(whiteout);
  3256. whiteout = NULL;
  3257. goto end_rename;
  3258. }
  3259. }
  3260. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3261. ext4_handle_sync(handle);
  3262. if (S_ISDIR(old.inode->i_mode)) {
  3263. if (new.inode) {
  3264. retval = -ENOTEMPTY;
  3265. if (!ext4_empty_dir(new.inode))
  3266. goto end_rename;
  3267. } else {
  3268. retval = -EMLINK;
  3269. if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
  3270. goto end_rename;
  3271. }
  3272. retval = ext4_rename_dir_prepare(handle, &old);
  3273. if (retval)
  3274. goto end_rename;
  3275. }
  3276. /*
  3277. * If we're renaming a file within an inline_data dir and adding or
  3278. * setting the new dirent causes a conversion from inline_data to
  3279. * extents/blockmap, we need to force the dirent delete code to
  3280. * re-read the directory, or else we end up trying to delete a dirent
  3281. * from what is now the extent tree root (or a block map).
  3282. */
  3283. force_reread = (new.dir->i_ino == old.dir->i_ino &&
  3284. ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
  3285. old_file_type = old.de->file_type;
  3286. if (whiteout) {
  3287. /*
  3288. * Do this before adding a new entry, so the old entry is sure
  3289. * to be still pointing to the valid old entry.
  3290. */
  3291. retval = ext4_setent(handle, &old, whiteout->i_ino,
  3292. EXT4_FT_CHRDEV);
  3293. if (retval)
  3294. goto end_rename;
  3295. ext4_mark_inode_dirty(handle, whiteout);
  3296. }
  3297. if (!new.bh) {
  3298. retval = ext4_add_entry(handle, new.dentry, old.inode);
  3299. if (retval)
  3300. goto end_rename;
  3301. } else {
  3302. retval = ext4_setent(handle, &new,
  3303. old.inode->i_ino, old_file_type);
  3304. if (retval)
  3305. goto end_rename;
  3306. }
  3307. if (force_reread)
  3308. force_reread = !ext4_test_inode_flag(new.dir,
  3309. EXT4_INODE_INLINE_DATA);
  3310. /*
  3311. * Like most other Unix systems, set the ctime for inodes on a
  3312. * rename.
  3313. */
  3314. old.inode->i_ctime = current_time(old.inode);
  3315. ext4_mark_inode_dirty(handle, old.inode);
  3316. if (!whiteout) {
  3317. /*
  3318. * ok, that's it
  3319. */
  3320. ext4_rename_delete(handle, &old, force_reread);
  3321. }
  3322. if (new.inode) {
  3323. ext4_dec_count(handle, new.inode);
  3324. new.inode->i_ctime = current_time(new.inode);
  3325. }
  3326. old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir);
  3327. ext4_update_dx_flag(old.dir);
  3328. if (old.dir_bh) {
  3329. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3330. if (retval)
  3331. goto end_rename;
  3332. ext4_dec_count(handle, old.dir);
  3333. if (new.inode) {
  3334. /* checked ext4_empty_dir above, can't have another
  3335. * parent, ext4_dec_count() won't work for many-linked
  3336. * dirs */
  3337. clear_nlink(new.inode);
  3338. } else {
  3339. ext4_inc_count(handle, new.dir);
  3340. ext4_update_dx_flag(new.dir);
  3341. ext4_mark_inode_dirty(handle, new.dir);
  3342. }
  3343. }
  3344. ext4_mark_inode_dirty(handle, old.dir);
  3345. if (new.inode) {
  3346. ext4_mark_inode_dirty(handle, new.inode);
  3347. if (!new.inode->i_nlink)
  3348. ext4_orphan_add(handle, new.inode);
  3349. }
  3350. retval = 0;
  3351. end_rename:
  3352. brelse(old.dir_bh);
  3353. brelse(old.bh);
  3354. brelse(new.bh);
  3355. if (whiteout) {
  3356. if (retval)
  3357. drop_nlink(whiteout);
  3358. unlock_new_inode(whiteout);
  3359. iput(whiteout);
  3360. }
  3361. if (handle)
  3362. ext4_journal_stop(handle);
  3363. return retval;
  3364. }
  3365. static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
  3366. struct inode *new_dir, struct dentry *new_dentry)
  3367. {
  3368. handle_t *handle = NULL;
  3369. struct ext4_renament old = {
  3370. .dir = old_dir,
  3371. .dentry = old_dentry,
  3372. .inode = d_inode(old_dentry),
  3373. };
  3374. struct ext4_renament new = {
  3375. .dir = new_dir,
  3376. .dentry = new_dentry,
  3377. .inode = d_inode(new_dentry),
  3378. };
  3379. u8 new_file_type;
  3380. int retval;
  3381. struct timespec64 ctime;
  3382. if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
  3383. !projid_eq(EXT4_I(new_dir)->i_projid,
  3384. EXT4_I(old_dentry->d_inode)->i_projid)) ||
  3385. (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
  3386. !projid_eq(EXT4_I(old_dir)->i_projid,
  3387. EXT4_I(new_dentry->d_inode)->i_projid)))
  3388. return -EXDEV;
  3389. retval = dquot_initialize(old.dir);
  3390. if (retval)
  3391. return retval;
  3392. retval = dquot_initialize(new.dir);
  3393. if (retval)
  3394. return retval;
  3395. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
  3396. &old.de, &old.inlined);
  3397. if (IS_ERR(old.bh))
  3398. return PTR_ERR(old.bh);
  3399. /*
  3400. * Check for inode number is _not_ due to possible IO errors.
  3401. * We might rmdir the source, keep it as pwd of some process
  3402. * and merrily kill the link to whatever was created under the
  3403. * same name. Goodbye sticky bit ;-<
  3404. */
  3405. retval = -ENOENT;
  3406. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  3407. goto end_rename;
  3408. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  3409. &new.de, &new.inlined);
  3410. if (IS_ERR(new.bh)) {
  3411. retval = PTR_ERR(new.bh);
  3412. new.bh = NULL;
  3413. goto end_rename;
  3414. }
  3415. /* RENAME_EXCHANGE case: old *and* new must both exist */
  3416. if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
  3417. goto end_rename;
  3418. handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
  3419. (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3420. 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
  3421. if (IS_ERR(handle)) {
  3422. retval = PTR_ERR(handle);
  3423. handle = NULL;
  3424. goto end_rename;
  3425. }
  3426. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3427. ext4_handle_sync(handle);
  3428. if (S_ISDIR(old.inode->i_mode)) {
  3429. old.is_dir = true;
  3430. retval = ext4_rename_dir_prepare(handle, &old);
  3431. if (retval)
  3432. goto end_rename;
  3433. }
  3434. if (S_ISDIR(new.inode->i_mode)) {
  3435. new.is_dir = true;
  3436. retval = ext4_rename_dir_prepare(handle, &new);
  3437. if (retval)
  3438. goto end_rename;
  3439. }
  3440. /*
  3441. * Other than the special case of overwriting a directory, parents'
  3442. * nlink only needs to be modified if this is a cross directory rename.
  3443. */
  3444. if (old.dir != new.dir && old.is_dir != new.is_dir) {
  3445. old.dir_nlink_delta = old.is_dir ? -1 : 1;
  3446. new.dir_nlink_delta = -old.dir_nlink_delta;
  3447. retval = -EMLINK;
  3448. if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
  3449. (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
  3450. goto end_rename;
  3451. }
  3452. new_file_type = new.de->file_type;
  3453. retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
  3454. if (retval)
  3455. goto end_rename;
  3456. retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
  3457. if (retval)
  3458. goto end_rename;
  3459. /*
  3460. * Like most other Unix systems, set the ctime for inodes on a
  3461. * rename.
  3462. */
  3463. ctime = current_time(old.inode);
  3464. old.inode->i_ctime = ctime;
  3465. new.inode->i_ctime = ctime;
  3466. ext4_mark_inode_dirty(handle, old.inode);
  3467. ext4_mark_inode_dirty(handle, new.inode);
  3468. if (old.dir_bh) {
  3469. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3470. if (retval)
  3471. goto end_rename;
  3472. }
  3473. if (new.dir_bh) {
  3474. retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
  3475. if (retval)
  3476. goto end_rename;
  3477. }
  3478. ext4_update_dir_count(handle, &old);
  3479. ext4_update_dir_count(handle, &new);
  3480. retval = 0;
  3481. end_rename:
  3482. brelse(old.dir_bh);
  3483. brelse(new.dir_bh);
  3484. brelse(old.bh);
  3485. brelse(new.bh);
  3486. if (handle)
  3487. ext4_journal_stop(handle);
  3488. return retval;
  3489. }
  3490. static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
  3491. struct inode *new_dir, struct dentry *new_dentry,
  3492. unsigned int flags)
  3493. {
  3494. int err;
  3495. if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb))))
  3496. return -EIO;
  3497. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  3498. return -EINVAL;
  3499. err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
  3500. flags);
  3501. if (err)
  3502. return err;
  3503. if (flags & RENAME_EXCHANGE) {
  3504. return ext4_cross_rename(old_dir, old_dentry,
  3505. new_dir, new_dentry);
  3506. }
  3507. return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  3508. }
  3509. /*
  3510. * directories can handle most operations...
  3511. */
  3512. const struct inode_operations ext4_dir_inode_operations = {
  3513. .create = ext4_create,
  3514. .lookup = ext4_lookup,
  3515. .link = ext4_link,
  3516. .unlink = ext4_unlink,
  3517. .symlink = ext4_symlink,
  3518. .mkdir = ext4_mkdir,
  3519. .rmdir = ext4_rmdir,
  3520. .mknod = ext4_mknod,
  3521. .tmpfile = ext4_tmpfile,
  3522. .rename = ext4_rename2,
  3523. .setattr = ext4_setattr,
  3524. .getattr = ext4_getattr,
  3525. .listxattr = ext4_listxattr,
  3526. .get_acl = ext4_get_acl,
  3527. .set_acl = ext4_set_acl,
  3528. .fiemap = ext4_fiemap,
  3529. };
  3530. const struct inode_operations ext4_special_inode_operations = {
  3531. .setattr = ext4_setattr,
  3532. .getattr = ext4_getattr,
  3533. .listxattr = ext4_listxattr,
  3534. .get_acl = ext4_get_acl,
  3535. .set_acl = ext4_set_acl,
  3536. };