xattr.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075
  1. /*
  2. * linux/fs/ext4/xattr.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. *
  6. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  7. * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
  8. * Extended attributes for symlinks and special files added per
  9. * suggestion of Luka Renko <luka.renko@hermes.si>.
  10. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  11. * Red Hat Inc.
  12. * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
  13. * and Andreas Gruenbacher <agruen@suse.de>.
  14. */
  15. /*
  16. * Extended attributes are stored directly in inodes (on file systems with
  17. * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
  18. * field contains the block number if an inode uses an additional block. All
  19. * attributes must fit in the inode and one additional block. Blocks that
  20. * contain the identical set of attributes may be shared among several inodes.
  21. * Identical blocks are detected by keeping a cache of blocks that have
  22. * recently been accessed.
  23. *
  24. * The attributes in inodes and on blocks have a different header; the entries
  25. * are stored in the same format:
  26. *
  27. * +------------------+
  28. * | header |
  29. * | entry 1 | |
  30. * | entry 2 | | growing downwards
  31. * | entry 3 | v
  32. * | four null bytes |
  33. * | . . . |
  34. * | value 1 | ^
  35. * | value 3 | | growing upwards
  36. * | value 2 | |
  37. * +------------------+
  38. *
  39. * The header is followed by multiple entry descriptors. In disk blocks, the
  40. * entry descriptors are kept sorted. In inodes, they are unsorted. The
  41. * attribute values are aligned to the end of the block in no specific order.
  42. *
  43. * Locking strategy
  44. * ----------------
  45. * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
  46. * EA blocks are only changed if they are exclusive to an inode, so
  47. * holding xattr_sem also means that nothing but the EA block's reference
  48. * count can change. Multiple writers to the same block are synchronized
  49. * by the buffer lock.
  50. */
  51. #include <linux/init.h>
  52. #include <linux/fs.h>
  53. #include <linux/slab.h>
  54. #include <linux/mbcache.h>
  55. #include <linux/quotaops.h>
  56. #include "ext4_jbd2.h"
  57. #include "ext4.h"
  58. #include "xattr.h"
  59. #include "acl.h"
  60. #ifdef EXT4_XATTR_DEBUG
  61. # define ea_idebug(inode, fmt, ...) \
  62. printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
  63. inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
  64. # define ea_bdebug(bh, fmt, ...) \
  65. printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
  66. bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
  67. #else
  68. # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  69. # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  70. #endif
  71. static void ext4_xattr_block_cache_insert(struct mb_cache *,
  72. struct buffer_head *);
  73. static struct buffer_head *
  74. ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
  75. struct mb_cache_entry **);
  76. static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
  77. size_t value_count);
  78. static void ext4_xattr_rehash(struct ext4_xattr_header *);
  79. static const struct xattr_handler * const ext4_xattr_handler_map[] = {
  80. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  81. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  82. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  83. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  84. #endif
  85. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  86. #ifdef CONFIG_EXT4_FS_SECURITY
  87. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  88. #endif
  89. };
  90. const struct xattr_handler *ext4_xattr_handlers[] = {
  91. &ext4_xattr_user_handler,
  92. &ext4_xattr_trusted_handler,
  93. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  94. &posix_acl_access_xattr_handler,
  95. &posix_acl_default_xattr_handler,
  96. #endif
  97. #ifdef CONFIG_EXT4_FS_SECURITY
  98. &ext4_xattr_security_handler,
  99. #endif
  100. NULL
  101. };
  102. #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
  103. inode->i_sb->s_fs_info)->s_ea_block_cache)
  104. #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
  105. inode->i_sb->s_fs_info)->s_ea_inode_cache)
  106. static int
  107. ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
  108. struct inode *inode);
  109. #ifdef CONFIG_LOCKDEP
  110. void ext4_xattr_inode_set_class(struct inode *ea_inode)
  111. {
  112. lockdep_set_subclass(&ea_inode->i_rwsem, 1);
  113. }
  114. #endif
  115. static __le32 ext4_xattr_block_csum(struct inode *inode,
  116. sector_t block_nr,
  117. struct ext4_xattr_header *hdr)
  118. {
  119. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  120. __u32 csum;
  121. __le64 dsk_block_nr = cpu_to_le64(block_nr);
  122. __u32 dummy_csum = 0;
  123. int offset = offsetof(struct ext4_xattr_header, h_checksum);
  124. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
  125. sizeof(dsk_block_nr));
  126. csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
  127. csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
  128. offset += sizeof(dummy_csum);
  129. csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
  130. EXT4_BLOCK_SIZE(inode->i_sb) - offset);
  131. return cpu_to_le32(csum);
  132. }
  133. static int ext4_xattr_block_csum_verify(struct inode *inode,
  134. struct buffer_head *bh)
  135. {
  136. struct ext4_xattr_header *hdr = BHDR(bh);
  137. int ret = 1;
  138. if (ext4_has_metadata_csum(inode->i_sb)) {
  139. lock_buffer(bh);
  140. ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
  141. bh->b_blocknr, hdr));
  142. unlock_buffer(bh);
  143. }
  144. return ret;
  145. }
  146. static void ext4_xattr_block_csum_set(struct inode *inode,
  147. struct buffer_head *bh)
  148. {
  149. if (ext4_has_metadata_csum(inode->i_sb))
  150. BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
  151. bh->b_blocknr, BHDR(bh));
  152. }
  153. static inline const struct xattr_handler *
  154. ext4_xattr_handler(int name_index)
  155. {
  156. const struct xattr_handler *handler = NULL;
  157. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  158. handler = ext4_xattr_handler_map[name_index];
  159. return handler;
  160. }
  161. static int
  162. ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
  163. void *value_start)
  164. {
  165. struct ext4_xattr_entry *e = entry;
  166. /* Find the end of the names list */
  167. while (!IS_LAST_ENTRY(e)) {
  168. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
  169. if ((void *)next >= end)
  170. return -EFSCORRUPTED;
  171. e = next;
  172. }
  173. /* Check the values */
  174. while (!IS_LAST_ENTRY(entry)) {
  175. if (entry->e_value_size != 0 &&
  176. entry->e_value_inum == 0) {
  177. u16 offs = le16_to_cpu(entry->e_value_offs);
  178. u32 size = le32_to_cpu(entry->e_value_size);
  179. void *value;
  180. /*
  181. * The value cannot overlap the names, and the value
  182. * with padding cannot extend beyond 'end'. Check both
  183. * the padded and unpadded sizes, since the size may
  184. * overflow to 0 when adding padding.
  185. */
  186. if (offs > end - value_start)
  187. return -EFSCORRUPTED;
  188. value = value_start + offs;
  189. if (value < (void *)e + sizeof(u32) ||
  190. size > end - value ||
  191. EXT4_XATTR_SIZE(size) > end - value)
  192. return -EFSCORRUPTED;
  193. }
  194. entry = EXT4_XATTR_NEXT(entry);
  195. }
  196. return 0;
  197. }
  198. static inline int
  199. ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
  200. {
  201. int error;
  202. if (buffer_verified(bh))
  203. return 0;
  204. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  205. BHDR(bh)->h_blocks != cpu_to_le32(1))
  206. return -EFSCORRUPTED;
  207. if (!ext4_xattr_block_csum_verify(inode, bh))
  208. return -EFSBADCRC;
  209. error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
  210. bh->b_data);
  211. if (!error)
  212. set_buffer_verified(bh);
  213. return error;
  214. }
  215. static int
  216. __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
  217. void *end, const char *function, unsigned int line)
  218. {
  219. int error = -EFSCORRUPTED;
  220. if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
  221. (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
  222. goto errout;
  223. error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
  224. errout:
  225. if (error)
  226. __ext4_error_inode(inode, function, line, 0,
  227. "corrupted in-inode xattr");
  228. return error;
  229. }
  230. #define xattr_check_inode(inode, header, end) \
  231. __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
  232. static int
  233. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  234. const char *name, int sorted)
  235. {
  236. struct ext4_xattr_entry *entry;
  237. size_t name_len;
  238. int cmp = 1;
  239. if (name == NULL)
  240. return -EINVAL;
  241. name_len = strlen(name);
  242. entry = *pentry;
  243. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  244. cmp = name_index - entry->e_name_index;
  245. if (!cmp)
  246. cmp = name_len - entry->e_name_len;
  247. if (!cmp)
  248. cmp = memcmp(name, entry->e_name, name_len);
  249. if (cmp <= 0 && (sorted || cmp == 0))
  250. break;
  251. }
  252. *pentry = entry;
  253. return cmp ? -ENODATA : 0;
  254. }
  255. static u32
  256. ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
  257. {
  258. return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
  259. }
  260. static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
  261. {
  262. return ((u64)ea_inode->i_ctime.tv_sec << 32) |
  263. ((u32)ea_inode->i_version);
  264. }
  265. static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
  266. {
  267. ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
  268. ea_inode->i_version = (u32)ref_count;
  269. }
  270. static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
  271. {
  272. return (u32)ea_inode->i_atime.tv_sec;
  273. }
  274. static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
  275. {
  276. ea_inode->i_atime.tv_sec = hash;
  277. }
  278. /*
  279. * Read the EA value from an inode.
  280. */
  281. static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
  282. {
  283. int blocksize = 1 << ea_inode->i_blkbits;
  284. int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
  285. int tail_size = (size % blocksize) ?: blocksize;
  286. struct buffer_head *bhs_inline[8];
  287. struct buffer_head **bhs = bhs_inline;
  288. int i, ret;
  289. if (bh_count > ARRAY_SIZE(bhs_inline)) {
  290. bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
  291. if (!bhs)
  292. return -ENOMEM;
  293. }
  294. ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
  295. true /* wait */, bhs);
  296. if (ret)
  297. goto free_bhs;
  298. for (i = 0; i < bh_count; i++) {
  299. /* There shouldn't be any holes in ea_inode. */
  300. if (!bhs[i]) {
  301. ret = -EFSCORRUPTED;
  302. goto put_bhs;
  303. }
  304. memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
  305. i < bh_count - 1 ? blocksize : tail_size);
  306. }
  307. ret = 0;
  308. put_bhs:
  309. for (i = 0; i < bh_count; i++)
  310. brelse(bhs[i]);
  311. free_bhs:
  312. if (bhs != bhs_inline)
  313. kfree(bhs);
  314. return ret;
  315. }
  316. static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
  317. struct inode **ea_inode)
  318. {
  319. struct inode *inode;
  320. int err;
  321. inode = ext4_iget(parent->i_sb, ea_ino);
  322. if (IS_ERR(inode)) {
  323. err = PTR_ERR(inode);
  324. ext4_error(parent->i_sb,
  325. "error while reading EA inode %lu err=%d", ea_ino,
  326. err);
  327. return err;
  328. }
  329. if (is_bad_inode(inode)) {
  330. ext4_error(parent->i_sb,
  331. "error while reading EA inode %lu is_bad_inode",
  332. ea_ino);
  333. err = -EIO;
  334. goto error;
  335. }
  336. if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
  337. ext4_error(parent->i_sb,
  338. "EA inode %lu does not have EXT4_EA_INODE_FL flag",
  339. ea_ino);
  340. err = -EINVAL;
  341. goto error;
  342. }
  343. *ea_inode = inode;
  344. return 0;
  345. error:
  346. iput(inode);
  347. return err;
  348. }
  349. static int
  350. ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
  351. struct ext4_xattr_entry *entry, void *buffer,
  352. size_t size)
  353. {
  354. u32 hash;
  355. /* Verify stored hash matches calculated hash. */
  356. hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
  357. if (hash != ext4_xattr_inode_get_hash(ea_inode))
  358. return -EFSCORRUPTED;
  359. if (entry) {
  360. __le32 e_hash, tmp_data;
  361. /* Verify entry hash. */
  362. tmp_data = cpu_to_le32(hash);
  363. e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
  364. &tmp_data, 1);
  365. if (e_hash != entry->e_hash)
  366. return -EFSCORRUPTED;
  367. }
  368. return 0;
  369. }
  370. #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
  371. /*
  372. * Read xattr value from the EA inode.
  373. */
  374. static int
  375. ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
  376. void *buffer, size_t size)
  377. {
  378. struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
  379. struct inode *ea_inode;
  380. int err;
  381. err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
  382. &ea_inode);
  383. if (err) {
  384. ea_inode = NULL;
  385. goto out;
  386. }
  387. if (i_size_read(ea_inode) != size) {
  388. ext4_warning_inode(ea_inode,
  389. "ea_inode file size=%llu entry size=%zu",
  390. i_size_read(ea_inode), size);
  391. err = -EFSCORRUPTED;
  392. goto out;
  393. }
  394. err = ext4_xattr_inode_read(ea_inode, buffer, size);
  395. if (err)
  396. goto out;
  397. err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer, size);
  398. /*
  399. * Compatibility check for old Lustre ea_inode implementation. Old
  400. * version does not have hash validation, but it has a backpointer
  401. * from ea_inode to the parent inode.
  402. */
  403. if (err == -EFSCORRUPTED) {
  404. if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != inode->i_ino ||
  405. ea_inode->i_generation != inode->i_generation) {
  406. ext4_warning_inode(ea_inode,
  407. "EA inode hash validation failed");
  408. goto out;
  409. }
  410. /* Do not add ea_inode to the cache. */
  411. ea_inode_cache = NULL;
  412. err = 0;
  413. } else if (err)
  414. goto out;
  415. if (ea_inode_cache)
  416. mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
  417. ext4_xattr_inode_get_hash(ea_inode),
  418. ea_inode->i_ino, true /* reusable */);
  419. out:
  420. iput(ea_inode);
  421. return err;
  422. }
  423. static int
  424. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  425. void *buffer, size_t buffer_size)
  426. {
  427. struct buffer_head *bh = NULL;
  428. struct ext4_xattr_entry *entry;
  429. size_t size;
  430. int error;
  431. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  432. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  433. name_index, name, buffer, (long)buffer_size);
  434. error = -ENODATA;
  435. if (!EXT4_I(inode)->i_file_acl)
  436. goto cleanup;
  437. ea_idebug(inode, "reading block %llu",
  438. (unsigned long long)EXT4_I(inode)->i_file_acl);
  439. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  440. if (!bh)
  441. goto cleanup;
  442. ea_bdebug(bh, "b_count=%d, refcount=%d",
  443. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  444. if (ext4_xattr_check_block(inode, bh)) {
  445. EXT4_ERROR_INODE(inode, "bad block %llu",
  446. EXT4_I(inode)->i_file_acl);
  447. error = -EFSCORRUPTED;
  448. goto cleanup;
  449. }
  450. ext4_xattr_block_cache_insert(ea_block_cache, bh);
  451. entry = BFIRST(bh);
  452. error = ext4_xattr_find_entry(&entry, name_index, name, 1);
  453. if (error)
  454. goto cleanup;
  455. size = le32_to_cpu(entry->e_value_size);
  456. if (buffer) {
  457. error = -ERANGE;
  458. if (size > buffer_size)
  459. goto cleanup;
  460. if (entry->e_value_inum) {
  461. error = ext4_xattr_inode_get(inode, entry, buffer,
  462. size);
  463. if (error)
  464. goto cleanup;
  465. } else {
  466. memcpy(buffer, bh->b_data +
  467. le16_to_cpu(entry->e_value_offs), size);
  468. }
  469. }
  470. error = size;
  471. cleanup:
  472. brelse(bh);
  473. return error;
  474. }
  475. int
  476. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  477. void *buffer, size_t buffer_size)
  478. {
  479. struct ext4_xattr_ibody_header *header;
  480. struct ext4_xattr_entry *entry;
  481. struct ext4_inode *raw_inode;
  482. struct ext4_iloc iloc;
  483. size_t size;
  484. void *end;
  485. int error;
  486. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  487. return -ENODATA;
  488. error = ext4_get_inode_loc(inode, &iloc);
  489. if (error)
  490. return error;
  491. raw_inode = ext4_raw_inode(&iloc);
  492. header = IHDR(inode, raw_inode);
  493. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  494. error = xattr_check_inode(inode, header, end);
  495. if (error)
  496. goto cleanup;
  497. entry = IFIRST(header);
  498. error = ext4_xattr_find_entry(&entry, name_index, name, 0);
  499. if (error)
  500. goto cleanup;
  501. size = le32_to_cpu(entry->e_value_size);
  502. if (buffer) {
  503. error = -ERANGE;
  504. if (size > buffer_size)
  505. goto cleanup;
  506. if (entry->e_value_inum) {
  507. error = ext4_xattr_inode_get(inode, entry, buffer,
  508. size);
  509. if (error)
  510. goto cleanup;
  511. } else {
  512. memcpy(buffer, (void *)IFIRST(header) +
  513. le16_to_cpu(entry->e_value_offs), size);
  514. }
  515. }
  516. error = size;
  517. cleanup:
  518. brelse(iloc.bh);
  519. return error;
  520. }
  521. /*
  522. * ext4_xattr_get()
  523. *
  524. * Copy an extended attribute into the buffer
  525. * provided, or compute the buffer size required.
  526. * Buffer is NULL to compute the size of the buffer required.
  527. *
  528. * Returns a negative error number on failure, or the number of bytes
  529. * used / required on success.
  530. */
  531. int
  532. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  533. void *buffer, size_t buffer_size)
  534. {
  535. int error;
  536. if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  537. return -EIO;
  538. if (strlen(name) > 255)
  539. return -ERANGE;
  540. down_read(&EXT4_I(inode)->xattr_sem);
  541. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  542. buffer_size);
  543. if (error == -ENODATA)
  544. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  545. buffer_size);
  546. up_read(&EXT4_I(inode)->xattr_sem);
  547. return error;
  548. }
  549. static int
  550. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  551. char *buffer, size_t buffer_size)
  552. {
  553. size_t rest = buffer_size;
  554. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  555. const struct xattr_handler *handler =
  556. ext4_xattr_handler(entry->e_name_index);
  557. if (handler && (!handler->list || handler->list(dentry))) {
  558. const char *prefix = handler->prefix ?: handler->name;
  559. size_t prefix_len = strlen(prefix);
  560. size_t size = prefix_len + entry->e_name_len + 1;
  561. if (buffer) {
  562. if (size > rest)
  563. return -ERANGE;
  564. memcpy(buffer, prefix, prefix_len);
  565. buffer += prefix_len;
  566. memcpy(buffer, entry->e_name, entry->e_name_len);
  567. buffer += entry->e_name_len;
  568. *buffer++ = 0;
  569. }
  570. rest -= size;
  571. }
  572. }
  573. return buffer_size - rest; /* total size */
  574. }
  575. static int
  576. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  577. {
  578. struct inode *inode = d_inode(dentry);
  579. struct buffer_head *bh = NULL;
  580. int error;
  581. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  582. buffer, (long)buffer_size);
  583. error = 0;
  584. if (!EXT4_I(inode)->i_file_acl)
  585. goto cleanup;
  586. ea_idebug(inode, "reading block %llu",
  587. (unsigned long long)EXT4_I(inode)->i_file_acl);
  588. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  589. error = -EIO;
  590. if (!bh)
  591. goto cleanup;
  592. ea_bdebug(bh, "b_count=%d, refcount=%d",
  593. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  594. if (ext4_xattr_check_block(inode, bh)) {
  595. EXT4_ERROR_INODE(inode, "bad block %llu",
  596. EXT4_I(inode)->i_file_acl);
  597. error = -EFSCORRUPTED;
  598. goto cleanup;
  599. }
  600. ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
  601. error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  602. cleanup:
  603. brelse(bh);
  604. return error;
  605. }
  606. static int
  607. ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  608. {
  609. struct inode *inode = d_inode(dentry);
  610. struct ext4_xattr_ibody_header *header;
  611. struct ext4_inode *raw_inode;
  612. struct ext4_iloc iloc;
  613. void *end;
  614. int error;
  615. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  616. return 0;
  617. error = ext4_get_inode_loc(inode, &iloc);
  618. if (error)
  619. return error;
  620. raw_inode = ext4_raw_inode(&iloc);
  621. header = IHDR(inode, raw_inode);
  622. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  623. error = xattr_check_inode(inode, header, end);
  624. if (error)
  625. goto cleanup;
  626. error = ext4_xattr_list_entries(dentry, IFIRST(header),
  627. buffer, buffer_size);
  628. cleanup:
  629. brelse(iloc.bh);
  630. return error;
  631. }
  632. /*
  633. * Inode operation listxattr()
  634. *
  635. * d_inode(dentry)->i_rwsem: don't care
  636. *
  637. * Copy a list of attribute names into the buffer
  638. * provided, or compute the buffer size required.
  639. * Buffer is NULL to compute the size of the buffer required.
  640. *
  641. * Returns a negative error number on failure, or the number of bytes
  642. * used / required on success.
  643. */
  644. ssize_t
  645. ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  646. {
  647. int ret, ret2;
  648. down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  649. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  650. if (ret < 0)
  651. goto errout;
  652. if (buffer) {
  653. buffer += ret;
  654. buffer_size -= ret;
  655. }
  656. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  657. if (ret < 0)
  658. goto errout;
  659. ret += ret2;
  660. errout:
  661. up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  662. return ret;
  663. }
  664. /*
  665. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  666. * not set, set it.
  667. */
  668. static void ext4_xattr_update_super_block(handle_t *handle,
  669. struct super_block *sb)
  670. {
  671. if (ext4_has_feature_xattr(sb))
  672. return;
  673. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  674. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  675. ext4_set_feature_xattr(sb);
  676. ext4_handle_dirty_super(handle, sb);
  677. }
  678. }
  679. int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
  680. {
  681. struct ext4_iloc iloc = { .bh = NULL };
  682. struct buffer_head *bh = NULL;
  683. struct ext4_inode *raw_inode;
  684. struct ext4_xattr_ibody_header *header;
  685. struct ext4_xattr_entry *entry;
  686. qsize_t ea_inode_refs = 0;
  687. void *end;
  688. int ret;
  689. lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
  690. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  691. ret = ext4_get_inode_loc(inode, &iloc);
  692. if (ret)
  693. goto out;
  694. raw_inode = ext4_raw_inode(&iloc);
  695. header = IHDR(inode, raw_inode);
  696. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  697. ret = xattr_check_inode(inode, header, end);
  698. if (ret)
  699. goto out;
  700. for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
  701. entry = EXT4_XATTR_NEXT(entry))
  702. if (entry->e_value_inum)
  703. ea_inode_refs++;
  704. }
  705. if (EXT4_I(inode)->i_file_acl) {
  706. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  707. if (!bh) {
  708. ret = -EIO;
  709. goto out;
  710. }
  711. if (ext4_xattr_check_block(inode, bh)) {
  712. ret = -EFSCORRUPTED;
  713. goto out;
  714. }
  715. for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
  716. entry = EXT4_XATTR_NEXT(entry))
  717. if (entry->e_value_inum)
  718. ea_inode_refs++;
  719. }
  720. *usage = ea_inode_refs + 1;
  721. ret = 0;
  722. out:
  723. brelse(iloc.bh);
  724. brelse(bh);
  725. return ret;
  726. }
  727. static inline size_t round_up_cluster(struct inode *inode, size_t length)
  728. {
  729. struct super_block *sb = inode->i_sb;
  730. size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
  731. inode->i_blkbits);
  732. size_t mask = ~(cluster_size - 1);
  733. return (length + cluster_size - 1) & mask;
  734. }
  735. static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
  736. {
  737. int err;
  738. err = dquot_alloc_inode(inode);
  739. if (err)
  740. return err;
  741. err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
  742. if (err)
  743. dquot_free_inode(inode);
  744. return err;
  745. }
  746. static void ext4_xattr_inode_free_quota(struct inode *inode, size_t len)
  747. {
  748. dquot_free_space_nodirty(inode, round_up_cluster(inode, len));
  749. dquot_free_inode(inode);
  750. }
  751. int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
  752. struct buffer_head *block_bh, size_t value_len,
  753. bool is_create)
  754. {
  755. int credits;
  756. int blocks;
  757. /*
  758. * 1) Owner inode update
  759. * 2) Ref count update on old xattr block
  760. * 3) new xattr block
  761. * 4) block bitmap update for new xattr block
  762. * 5) group descriptor for new xattr block
  763. * 6) block bitmap update for old xattr block
  764. * 7) group descriptor for old block
  765. *
  766. * 6 & 7 can happen if we have two racing threads T_a and T_b
  767. * which are each trying to set an xattr on inodes I_a and I_b
  768. * which were both initially sharing an xattr block.
  769. */
  770. credits = 7;
  771. /* Quota updates. */
  772. credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
  773. /*
  774. * In case of inline data, we may push out the data to a block,
  775. * so we need to reserve credits for this eventuality
  776. */
  777. if (inode && ext4_has_inline_data(inode))
  778. credits += ext4_writepage_trans_blocks(inode) + 1;
  779. /* We are done if ea_inode feature is not enabled. */
  780. if (!ext4_has_feature_ea_inode(sb))
  781. return credits;
  782. /* New ea_inode, inode map, block bitmap, group descriptor. */
  783. credits += 4;
  784. /* Data blocks. */
  785. blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  786. /* Indirection block or one level of extent tree. */
  787. blocks += 1;
  788. /* Block bitmap and group descriptor updates for each block. */
  789. credits += blocks * 2;
  790. /* Blocks themselves. */
  791. credits += blocks;
  792. if (!is_create) {
  793. /* Dereference ea_inode holding old xattr value.
  794. * Old ea_inode, inode map, block bitmap, group descriptor.
  795. */
  796. credits += 4;
  797. /* Data blocks for old ea_inode. */
  798. blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
  799. /* Indirection block or one level of extent tree for old
  800. * ea_inode.
  801. */
  802. blocks += 1;
  803. /* Block bitmap and group descriptor updates for each block. */
  804. credits += blocks * 2;
  805. }
  806. /* We may need to clone the existing xattr block in which case we need
  807. * to increment ref counts for existing ea_inodes referenced by it.
  808. */
  809. if (block_bh) {
  810. struct ext4_xattr_entry *entry = BFIRST(block_bh);
  811. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
  812. if (entry->e_value_inum)
  813. /* Ref count update on ea_inode. */
  814. credits += 1;
  815. }
  816. return credits;
  817. }
  818. static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
  819. int credits, struct buffer_head *bh,
  820. bool dirty, bool block_csum)
  821. {
  822. int error;
  823. if (!ext4_handle_valid(handle))
  824. return 0;
  825. if (handle->h_buffer_credits >= credits)
  826. return 0;
  827. error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
  828. if (!error)
  829. return 0;
  830. if (error < 0) {
  831. ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
  832. return error;
  833. }
  834. if (bh && dirty) {
  835. if (block_csum)
  836. ext4_xattr_block_csum_set(inode, bh);
  837. error = ext4_handle_dirty_metadata(handle, NULL, bh);
  838. if (error) {
  839. ext4_warning(inode->i_sb, "Handle metadata (error %d)",
  840. error);
  841. return error;
  842. }
  843. }
  844. error = ext4_journal_restart(handle, credits);
  845. if (error) {
  846. ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
  847. return error;
  848. }
  849. if (bh) {
  850. error = ext4_journal_get_write_access(handle, bh);
  851. if (error) {
  852. ext4_warning(inode->i_sb,
  853. "Get write access failed (error %d)",
  854. error);
  855. return error;
  856. }
  857. }
  858. return 0;
  859. }
  860. static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
  861. int ref_change)
  862. {
  863. struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
  864. struct ext4_iloc iloc;
  865. s64 ref_count;
  866. u32 hash;
  867. int ret;
  868. inode_lock(ea_inode);
  869. ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
  870. if (ret) {
  871. iloc.bh = NULL;
  872. goto out;
  873. }
  874. ref_count = ext4_xattr_inode_get_ref(ea_inode);
  875. ref_count += ref_change;
  876. ext4_xattr_inode_set_ref(ea_inode, ref_count);
  877. if (ref_change > 0) {
  878. WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
  879. ea_inode->i_ino, ref_count);
  880. if (ref_count == 1) {
  881. WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
  882. ea_inode->i_ino, ea_inode->i_nlink);
  883. set_nlink(ea_inode, 1);
  884. ext4_orphan_del(handle, ea_inode);
  885. if (ea_inode_cache) {
  886. hash = ext4_xattr_inode_get_hash(ea_inode);
  887. mb_cache_entry_create(ea_inode_cache,
  888. GFP_NOFS, hash,
  889. ea_inode->i_ino,
  890. true /* reusable */);
  891. }
  892. }
  893. } else {
  894. WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
  895. ea_inode->i_ino, ref_count);
  896. if (ref_count == 0) {
  897. WARN_ONCE(ea_inode->i_nlink != 1,
  898. "EA inode %lu i_nlink=%u",
  899. ea_inode->i_ino, ea_inode->i_nlink);
  900. clear_nlink(ea_inode);
  901. ext4_orphan_add(handle, ea_inode);
  902. if (ea_inode_cache) {
  903. hash = ext4_xattr_inode_get_hash(ea_inode);
  904. mb_cache_entry_delete(ea_inode_cache, hash,
  905. ea_inode->i_ino);
  906. }
  907. }
  908. }
  909. ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
  910. iloc.bh = NULL;
  911. if (ret)
  912. ext4_warning_inode(ea_inode,
  913. "ext4_mark_iloc_dirty() failed ret=%d", ret);
  914. out:
  915. brelse(iloc.bh);
  916. inode_unlock(ea_inode);
  917. return ret;
  918. }
  919. static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
  920. {
  921. return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
  922. }
  923. static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
  924. {
  925. return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
  926. }
  927. static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
  928. struct ext4_xattr_entry *first)
  929. {
  930. struct inode *ea_inode;
  931. struct ext4_xattr_entry *entry;
  932. struct ext4_xattr_entry *failed_entry;
  933. unsigned int ea_ino;
  934. int err, saved_err;
  935. for (entry = first; !IS_LAST_ENTRY(entry);
  936. entry = EXT4_XATTR_NEXT(entry)) {
  937. if (!entry->e_value_inum)
  938. continue;
  939. ea_ino = le32_to_cpu(entry->e_value_inum);
  940. err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
  941. if (err)
  942. goto cleanup;
  943. err = ext4_xattr_inode_inc_ref(handle, ea_inode);
  944. if (err) {
  945. ext4_warning_inode(ea_inode, "inc ref error %d", err);
  946. iput(ea_inode);
  947. goto cleanup;
  948. }
  949. iput(ea_inode);
  950. }
  951. return 0;
  952. cleanup:
  953. saved_err = err;
  954. failed_entry = entry;
  955. for (entry = first; entry != failed_entry;
  956. entry = EXT4_XATTR_NEXT(entry)) {
  957. if (!entry->e_value_inum)
  958. continue;
  959. ea_ino = le32_to_cpu(entry->e_value_inum);
  960. err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
  961. if (err) {
  962. ext4_warning(parent->i_sb,
  963. "cleanup ea_ino %u iget error %d", ea_ino,
  964. err);
  965. continue;
  966. }
  967. err = ext4_xattr_inode_dec_ref(handle, ea_inode);
  968. if (err)
  969. ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
  970. err);
  971. iput(ea_inode);
  972. }
  973. return saved_err;
  974. }
  975. static void
  976. ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
  977. struct buffer_head *bh,
  978. struct ext4_xattr_entry *first, bool block_csum,
  979. struct ext4_xattr_inode_array **ea_inode_array,
  980. int extra_credits, bool skip_quota)
  981. {
  982. struct inode *ea_inode;
  983. struct ext4_xattr_entry *entry;
  984. bool dirty = false;
  985. unsigned int ea_ino;
  986. int err;
  987. int credits;
  988. /* One credit for dec ref on ea_inode, one for orphan list addition, */
  989. credits = 2 + extra_credits;
  990. for (entry = first; !IS_LAST_ENTRY(entry);
  991. entry = EXT4_XATTR_NEXT(entry)) {
  992. if (!entry->e_value_inum)
  993. continue;
  994. ea_ino = le32_to_cpu(entry->e_value_inum);
  995. err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
  996. if (err)
  997. continue;
  998. err = ext4_expand_inode_array(ea_inode_array, ea_inode);
  999. if (err) {
  1000. ext4_warning_inode(ea_inode,
  1001. "Expand inode array err=%d", err);
  1002. iput(ea_inode);
  1003. continue;
  1004. }
  1005. err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
  1006. dirty, block_csum);
  1007. if (err) {
  1008. ext4_warning_inode(ea_inode, "Ensure credits err=%d",
  1009. err);
  1010. continue;
  1011. }
  1012. err = ext4_xattr_inode_dec_ref(handle, ea_inode);
  1013. if (err) {
  1014. ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
  1015. err);
  1016. continue;
  1017. }
  1018. if (!skip_quota)
  1019. ext4_xattr_inode_free_quota(parent,
  1020. le32_to_cpu(entry->e_value_size));
  1021. /*
  1022. * Forget about ea_inode within the same transaction that
  1023. * decrements the ref count. This avoids duplicate decrements in
  1024. * case the rest of the work spills over to subsequent
  1025. * transactions.
  1026. */
  1027. entry->e_value_inum = 0;
  1028. entry->e_value_size = 0;
  1029. dirty = true;
  1030. }
  1031. if (dirty) {
  1032. /*
  1033. * Note that we are deliberately skipping csum calculation for
  1034. * the final update because we do not expect any journal
  1035. * restarts until xattr block is freed.
  1036. */
  1037. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  1038. if (err)
  1039. ext4_warning_inode(parent,
  1040. "handle dirty metadata err=%d", err);
  1041. }
  1042. }
  1043. /*
  1044. * Release the xattr block BH: If the reference count is > 1, decrement it;
  1045. * otherwise free the block.
  1046. */
  1047. static void
  1048. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  1049. struct buffer_head *bh,
  1050. struct ext4_xattr_inode_array **ea_inode_array,
  1051. int extra_credits)
  1052. {
  1053. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  1054. u32 hash, ref;
  1055. int error = 0;
  1056. BUFFER_TRACE(bh, "get_write_access");
  1057. error = ext4_journal_get_write_access(handle, bh);
  1058. if (error)
  1059. goto out;
  1060. lock_buffer(bh);
  1061. hash = le32_to_cpu(BHDR(bh)->h_hash);
  1062. ref = le32_to_cpu(BHDR(bh)->h_refcount);
  1063. if (ref == 1) {
  1064. ea_bdebug(bh, "refcount now=0; freeing");
  1065. /*
  1066. * This must happen under buffer lock for
  1067. * ext4_xattr_block_set() to reliably detect freed block
  1068. */
  1069. if (ea_block_cache)
  1070. mb_cache_entry_delete(ea_block_cache, hash,
  1071. bh->b_blocknr);
  1072. get_bh(bh);
  1073. unlock_buffer(bh);
  1074. if (ext4_has_feature_ea_inode(inode->i_sb))
  1075. ext4_xattr_inode_dec_ref_all(handle, inode, bh,
  1076. BFIRST(bh),
  1077. true /* block_csum */,
  1078. ea_inode_array,
  1079. extra_credits,
  1080. true /* skip_quota */);
  1081. ext4_free_blocks(handle, inode, bh, 0, 1,
  1082. EXT4_FREE_BLOCKS_METADATA |
  1083. EXT4_FREE_BLOCKS_FORGET);
  1084. } else {
  1085. ref--;
  1086. BHDR(bh)->h_refcount = cpu_to_le32(ref);
  1087. if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
  1088. struct mb_cache_entry *ce;
  1089. if (ea_block_cache) {
  1090. ce = mb_cache_entry_get(ea_block_cache, hash,
  1091. bh->b_blocknr);
  1092. if (ce) {
  1093. ce->e_reusable = 1;
  1094. mb_cache_entry_put(ea_block_cache, ce);
  1095. }
  1096. }
  1097. }
  1098. ext4_xattr_block_csum_set(inode, bh);
  1099. /*
  1100. * Beware of this ugliness: Releasing of xattr block references
  1101. * from different inodes can race and so we have to protect
  1102. * from a race where someone else frees the block (and releases
  1103. * its journal_head) before we are done dirtying the buffer. In
  1104. * nojournal mode this race is harmless and we actually cannot
  1105. * call ext4_handle_dirty_metadata() with locked buffer as
  1106. * that function can call sync_dirty_buffer() so for that case
  1107. * we handle the dirtying after unlocking the buffer.
  1108. */
  1109. if (ext4_handle_valid(handle))
  1110. error = ext4_handle_dirty_metadata(handle, inode, bh);
  1111. unlock_buffer(bh);
  1112. if (!ext4_handle_valid(handle))
  1113. error = ext4_handle_dirty_metadata(handle, inode, bh);
  1114. if (IS_SYNC(inode))
  1115. ext4_handle_sync(handle);
  1116. dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
  1117. ea_bdebug(bh, "refcount now=%d; releasing",
  1118. le32_to_cpu(BHDR(bh)->h_refcount));
  1119. }
  1120. out:
  1121. ext4_std_error(inode->i_sb, error);
  1122. return;
  1123. }
  1124. /*
  1125. * Find the available free space for EAs. This also returns the total number of
  1126. * bytes used by EA entries.
  1127. */
  1128. static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
  1129. size_t *min_offs, void *base, int *total)
  1130. {
  1131. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1132. if (!last->e_value_inum && last->e_value_size) {
  1133. size_t offs = le16_to_cpu(last->e_value_offs);
  1134. if (offs < *min_offs)
  1135. *min_offs = offs;
  1136. }
  1137. if (total)
  1138. *total += EXT4_XATTR_LEN(last->e_name_len);
  1139. }
  1140. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  1141. }
  1142. /*
  1143. * Write the value of the EA in an inode.
  1144. */
  1145. static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
  1146. const void *buf, int bufsize)
  1147. {
  1148. struct buffer_head *bh = NULL;
  1149. unsigned long block = 0;
  1150. int blocksize = ea_inode->i_sb->s_blocksize;
  1151. int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
  1152. int csize, wsize = 0;
  1153. int ret = 0;
  1154. int retries = 0;
  1155. retry:
  1156. while (ret >= 0 && ret < max_blocks) {
  1157. struct ext4_map_blocks map;
  1158. map.m_lblk = block += ret;
  1159. map.m_len = max_blocks -= ret;
  1160. ret = ext4_map_blocks(handle, ea_inode, &map,
  1161. EXT4_GET_BLOCKS_CREATE);
  1162. if (ret <= 0) {
  1163. ext4_mark_inode_dirty(handle, ea_inode);
  1164. if (ret == -ENOSPC &&
  1165. ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
  1166. ret = 0;
  1167. goto retry;
  1168. }
  1169. break;
  1170. }
  1171. }
  1172. if (ret < 0)
  1173. return ret;
  1174. block = 0;
  1175. while (wsize < bufsize) {
  1176. if (bh != NULL)
  1177. brelse(bh);
  1178. csize = (bufsize - wsize) > blocksize ? blocksize :
  1179. bufsize - wsize;
  1180. bh = ext4_getblk(handle, ea_inode, block, 0);
  1181. if (IS_ERR(bh))
  1182. return PTR_ERR(bh);
  1183. ret = ext4_journal_get_write_access(handle, bh);
  1184. if (ret)
  1185. goto out;
  1186. memcpy(bh->b_data, buf, csize);
  1187. set_buffer_uptodate(bh);
  1188. ext4_handle_dirty_metadata(handle, ea_inode, bh);
  1189. buf += csize;
  1190. wsize += csize;
  1191. block += 1;
  1192. }
  1193. inode_lock(ea_inode);
  1194. i_size_write(ea_inode, wsize);
  1195. ext4_update_i_disksize(ea_inode, wsize);
  1196. inode_unlock(ea_inode);
  1197. ext4_mark_inode_dirty(handle, ea_inode);
  1198. out:
  1199. brelse(bh);
  1200. return ret;
  1201. }
  1202. /*
  1203. * Create an inode to store the value of a large EA.
  1204. */
  1205. static struct inode *ext4_xattr_inode_create(handle_t *handle,
  1206. struct inode *inode, u32 hash)
  1207. {
  1208. struct inode *ea_inode = NULL;
  1209. uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
  1210. int err;
  1211. /*
  1212. * Let the next inode be the goal, so we try and allocate the EA inode
  1213. * in the same group, or nearby one.
  1214. */
  1215. ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
  1216. S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
  1217. EXT4_EA_INODE_FL);
  1218. if (!IS_ERR(ea_inode)) {
  1219. ea_inode->i_op = &ext4_file_inode_operations;
  1220. ea_inode->i_fop = &ext4_file_operations;
  1221. ext4_set_aops(ea_inode);
  1222. ext4_xattr_inode_set_class(ea_inode);
  1223. unlock_new_inode(ea_inode);
  1224. ext4_xattr_inode_set_ref(ea_inode, 1);
  1225. ext4_xattr_inode_set_hash(ea_inode, hash);
  1226. err = ext4_mark_inode_dirty(handle, ea_inode);
  1227. if (!err)
  1228. err = ext4_inode_attach_jinode(ea_inode);
  1229. if (err) {
  1230. iput(ea_inode);
  1231. return ERR_PTR(err);
  1232. }
  1233. /*
  1234. * Xattr inodes are shared therefore quota charging is performed
  1235. * at a higher level.
  1236. */
  1237. dquot_free_inode(ea_inode);
  1238. dquot_drop(ea_inode);
  1239. inode_lock(ea_inode);
  1240. ea_inode->i_flags |= S_NOQUOTA;
  1241. inode_unlock(ea_inode);
  1242. }
  1243. return ea_inode;
  1244. }
  1245. static struct inode *
  1246. ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
  1247. size_t value_len, u32 hash)
  1248. {
  1249. struct inode *ea_inode;
  1250. struct mb_cache_entry *ce;
  1251. struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
  1252. void *ea_data;
  1253. if (!ea_inode_cache)
  1254. return NULL;
  1255. ce = mb_cache_entry_find_first(ea_inode_cache, hash);
  1256. if (!ce)
  1257. return NULL;
  1258. ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
  1259. if (!ea_data) {
  1260. mb_cache_entry_put(ea_inode_cache, ce);
  1261. return NULL;
  1262. }
  1263. while (ce) {
  1264. ea_inode = ext4_iget(inode->i_sb, ce->e_value);
  1265. if (!IS_ERR(ea_inode) &&
  1266. !is_bad_inode(ea_inode) &&
  1267. (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
  1268. i_size_read(ea_inode) == value_len &&
  1269. !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
  1270. !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
  1271. value_len) &&
  1272. !memcmp(value, ea_data, value_len)) {
  1273. mb_cache_entry_touch(ea_inode_cache, ce);
  1274. mb_cache_entry_put(ea_inode_cache, ce);
  1275. kvfree(ea_data);
  1276. return ea_inode;
  1277. }
  1278. if (!IS_ERR(ea_inode))
  1279. iput(ea_inode);
  1280. ce = mb_cache_entry_find_next(ea_inode_cache, ce);
  1281. }
  1282. kvfree(ea_data);
  1283. return NULL;
  1284. }
  1285. /*
  1286. * Add value of the EA in an inode.
  1287. */
  1288. static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
  1289. const void *value, size_t value_len,
  1290. struct inode **ret_inode)
  1291. {
  1292. struct inode *ea_inode;
  1293. u32 hash;
  1294. int err;
  1295. hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
  1296. ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
  1297. if (ea_inode) {
  1298. err = ext4_xattr_inode_inc_ref(handle, ea_inode);
  1299. if (err) {
  1300. iput(ea_inode);
  1301. return err;
  1302. }
  1303. *ret_inode = ea_inode;
  1304. return 0;
  1305. }
  1306. /* Create an inode for the EA value */
  1307. ea_inode = ext4_xattr_inode_create(handle, inode, hash);
  1308. if (IS_ERR(ea_inode))
  1309. return PTR_ERR(ea_inode);
  1310. err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
  1311. if (err) {
  1312. ext4_xattr_inode_dec_ref(handle, ea_inode);
  1313. iput(ea_inode);
  1314. return err;
  1315. }
  1316. if (EA_INODE_CACHE(inode))
  1317. mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
  1318. ea_inode->i_ino, true /* reusable */);
  1319. *ret_inode = ea_inode;
  1320. return 0;
  1321. }
  1322. /*
  1323. * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
  1324. * feature is enabled.
  1325. */
  1326. #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
  1327. static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
  1328. struct ext4_xattr_search *s,
  1329. handle_t *handle, struct inode *inode,
  1330. bool is_block)
  1331. {
  1332. struct ext4_xattr_entry *last;
  1333. struct ext4_xattr_entry *here = s->here;
  1334. size_t min_offs = s->end - s->base, name_len = strlen(i->name);
  1335. int in_inode = i->in_inode;
  1336. struct inode *old_ea_inode = NULL;
  1337. struct inode *new_ea_inode = NULL;
  1338. size_t old_size, new_size;
  1339. int ret;
  1340. /* Space used by old and new values. */
  1341. old_size = (!s->not_found && !here->e_value_inum) ?
  1342. EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
  1343. new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
  1344. /*
  1345. * Optimization for the simple case when old and new values have the
  1346. * same padded sizes. Not applicable if external inodes are involved.
  1347. */
  1348. if (new_size && new_size == old_size) {
  1349. size_t offs = le16_to_cpu(here->e_value_offs);
  1350. void *val = s->base + offs;
  1351. here->e_value_size = cpu_to_le32(i->value_len);
  1352. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  1353. memset(val, 0, new_size);
  1354. } else {
  1355. memcpy(val, i->value, i->value_len);
  1356. /* Clear padding bytes. */
  1357. memset(val + i->value_len, 0, new_size - i->value_len);
  1358. }
  1359. return 0;
  1360. }
  1361. /* Compute min_offs and last. */
  1362. last = s->first;
  1363. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1364. if (!last->e_value_inum && last->e_value_size) {
  1365. size_t offs = le16_to_cpu(last->e_value_offs);
  1366. if (offs < min_offs)
  1367. min_offs = offs;
  1368. }
  1369. }
  1370. /* Check whether we have enough space. */
  1371. if (i->value) {
  1372. size_t free;
  1373. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  1374. if (!s->not_found)
  1375. free += EXT4_XATTR_LEN(name_len) + old_size;
  1376. if (free < EXT4_XATTR_LEN(name_len) + new_size) {
  1377. ret = -ENOSPC;
  1378. goto out;
  1379. }
  1380. /*
  1381. * If storing the value in an external inode is an option,
  1382. * reserve space for xattr entries/names in the external
  1383. * attribute block so that a long value does not occupy the
  1384. * whole space and prevent futher entries being added.
  1385. */
  1386. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  1387. new_size && is_block &&
  1388. (min_offs + old_size - new_size) <
  1389. EXT4_XATTR_BLOCK_RESERVE(inode)) {
  1390. ret = -ENOSPC;
  1391. goto out;
  1392. }
  1393. }
  1394. /*
  1395. * Getting access to old and new ea inodes is subject to failures.
  1396. * Finish that work before doing any modifications to the xattr data.
  1397. */
  1398. if (!s->not_found && here->e_value_inum) {
  1399. ret = ext4_xattr_inode_iget(inode,
  1400. le32_to_cpu(here->e_value_inum),
  1401. &old_ea_inode);
  1402. if (ret) {
  1403. old_ea_inode = NULL;
  1404. goto out;
  1405. }
  1406. }
  1407. if (i->value && in_inode) {
  1408. WARN_ON_ONCE(!i->value_len);
  1409. ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
  1410. if (ret)
  1411. goto out;
  1412. ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
  1413. i->value_len,
  1414. &new_ea_inode);
  1415. if (ret) {
  1416. new_ea_inode = NULL;
  1417. ext4_xattr_inode_free_quota(inode, i->value_len);
  1418. goto out;
  1419. }
  1420. }
  1421. if (old_ea_inode) {
  1422. /* We are ready to release ref count on the old_ea_inode. */
  1423. ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
  1424. if (ret) {
  1425. /* Release newly required ref count on new_ea_inode. */
  1426. if (new_ea_inode) {
  1427. int err;
  1428. err = ext4_xattr_inode_dec_ref(handle,
  1429. new_ea_inode);
  1430. if (err)
  1431. ext4_warning_inode(new_ea_inode,
  1432. "dec ref new_ea_inode err=%d",
  1433. err);
  1434. ext4_xattr_inode_free_quota(inode,
  1435. i->value_len);
  1436. }
  1437. goto out;
  1438. }
  1439. ext4_xattr_inode_free_quota(inode,
  1440. le32_to_cpu(here->e_value_size));
  1441. }
  1442. /* No failures allowed past this point. */
  1443. if (!s->not_found && here->e_value_offs) {
  1444. /* Remove the old value. */
  1445. void *first_val = s->base + min_offs;
  1446. size_t offs = le16_to_cpu(here->e_value_offs);
  1447. void *val = s->base + offs;
  1448. memmove(first_val + old_size, first_val, val - first_val);
  1449. memset(first_val, 0, old_size);
  1450. min_offs += old_size;
  1451. /* Adjust all value offsets. */
  1452. last = s->first;
  1453. while (!IS_LAST_ENTRY(last)) {
  1454. size_t o = le16_to_cpu(last->e_value_offs);
  1455. if (!last->e_value_inum &&
  1456. last->e_value_size && o < offs)
  1457. last->e_value_offs = cpu_to_le16(o + old_size);
  1458. last = EXT4_XATTR_NEXT(last);
  1459. }
  1460. }
  1461. if (!i->value) {
  1462. /* Remove old name. */
  1463. size_t size = EXT4_XATTR_LEN(name_len);
  1464. last = ENTRY((void *)last - size);
  1465. memmove(here, (void *)here + size,
  1466. (void *)last - (void *)here + sizeof(__u32));
  1467. memset(last, 0, size);
  1468. } else if (s->not_found) {
  1469. /* Insert new name. */
  1470. size_t size = EXT4_XATTR_LEN(name_len);
  1471. size_t rest = (void *)last - (void *)here + sizeof(__u32);
  1472. memmove((void *)here + size, here, rest);
  1473. memset(here, 0, size);
  1474. here->e_name_index = i->name_index;
  1475. here->e_name_len = name_len;
  1476. memcpy(here->e_name, i->name, name_len);
  1477. } else {
  1478. /* This is an update, reset value info. */
  1479. here->e_value_inum = 0;
  1480. here->e_value_offs = 0;
  1481. here->e_value_size = 0;
  1482. }
  1483. if (i->value) {
  1484. /* Insert new value. */
  1485. if (in_inode) {
  1486. here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
  1487. } else if (i->value_len) {
  1488. void *val = s->base + min_offs - new_size;
  1489. here->e_value_offs = cpu_to_le16(min_offs - new_size);
  1490. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  1491. memset(val, 0, new_size);
  1492. } else {
  1493. memcpy(val, i->value, i->value_len);
  1494. /* Clear padding bytes. */
  1495. memset(val + i->value_len, 0,
  1496. new_size - i->value_len);
  1497. }
  1498. }
  1499. here->e_value_size = cpu_to_le32(i->value_len);
  1500. }
  1501. if (i->value) {
  1502. __le32 hash = 0;
  1503. /* Entry hash calculation. */
  1504. if (in_inode) {
  1505. __le32 crc32c_hash;
  1506. /*
  1507. * Feed crc32c hash instead of the raw value for entry
  1508. * hash calculation. This is to avoid walking
  1509. * potentially long value buffer again.
  1510. */
  1511. crc32c_hash = cpu_to_le32(
  1512. ext4_xattr_inode_get_hash(new_ea_inode));
  1513. hash = ext4_xattr_hash_entry(here->e_name,
  1514. here->e_name_len,
  1515. &crc32c_hash, 1);
  1516. } else if (is_block) {
  1517. __le32 *value = s->base + min_offs - new_size;
  1518. hash = ext4_xattr_hash_entry(here->e_name,
  1519. here->e_name_len, value,
  1520. new_size >> 2);
  1521. }
  1522. here->e_hash = hash;
  1523. }
  1524. if (is_block)
  1525. ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
  1526. ret = 0;
  1527. out:
  1528. iput(old_ea_inode);
  1529. iput(new_ea_inode);
  1530. return ret;
  1531. }
  1532. struct ext4_xattr_block_find {
  1533. struct ext4_xattr_search s;
  1534. struct buffer_head *bh;
  1535. };
  1536. static int
  1537. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  1538. struct ext4_xattr_block_find *bs)
  1539. {
  1540. struct super_block *sb = inode->i_sb;
  1541. int error;
  1542. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  1543. i->name_index, i->name, i->value, (long)i->value_len);
  1544. if (EXT4_I(inode)->i_file_acl) {
  1545. /* The inode already has an extended attribute block. */
  1546. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  1547. error = -EIO;
  1548. if (!bs->bh)
  1549. goto cleanup;
  1550. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  1551. atomic_read(&(bs->bh->b_count)),
  1552. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  1553. if (ext4_xattr_check_block(inode, bs->bh)) {
  1554. EXT4_ERROR_INODE(inode, "bad block %llu",
  1555. EXT4_I(inode)->i_file_acl);
  1556. error = -EFSCORRUPTED;
  1557. goto cleanup;
  1558. }
  1559. /* Find the named attribute. */
  1560. bs->s.base = BHDR(bs->bh);
  1561. bs->s.first = BFIRST(bs->bh);
  1562. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  1563. bs->s.here = bs->s.first;
  1564. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  1565. i->name, 1);
  1566. if (error && error != -ENODATA)
  1567. goto cleanup;
  1568. bs->s.not_found = error;
  1569. }
  1570. error = 0;
  1571. cleanup:
  1572. return error;
  1573. }
  1574. static int
  1575. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  1576. struct ext4_xattr_info *i,
  1577. struct ext4_xattr_block_find *bs)
  1578. {
  1579. struct super_block *sb = inode->i_sb;
  1580. struct buffer_head *new_bh = NULL;
  1581. struct ext4_xattr_search s_copy = bs->s;
  1582. struct ext4_xattr_search *s = &s_copy;
  1583. struct mb_cache_entry *ce = NULL;
  1584. int error = 0;
  1585. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  1586. struct inode *ea_inode = NULL;
  1587. size_t old_ea_inode_size = 0;
  1588. #define header(x) ((struct ext4_xattr_header *)(x))
  1589. if (s->base) {
  1590. BUFFER_TRACE(bs->bh, "get_write_access");
  1591. error = ext4_journal_get_write_access(handle, bs->bh);
  1592. if (error)
  1593. goto cleanup;
  1594. lock_buffer(bs->bh);
  1595. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  1596. __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
  1597. /*
  1598. * This must happen under buffer lock for
  1599. * ext4_xattr_block_set() to reliably detect modified
  1600. * block
  1601. */
  1602. if (ea_block_cache)
  1603. mb_cache_entry_delete(ea_block_cache, hash,
  1604. bs->bh->b_blocknr);
  1605. ea_bdebug(bs->bh, "modifying in-place");
  1606. error = ext4_xattr_set_entry(i, s, handle, inode,
  1607. true /* is_block */);
  1608. ext4_xattr_block_csum_set(inode, bs->bh);
  1609. unlock_buffer(bs->bh);
  1610. if (error == -EFSCORRUPTED)
  1611. goto bad_block;
  1612. if (!error)
  1613. error = ext4_handle_dirty_metadata(handle,
  1614. inode,
  1615. bs->bh);
  1616. if (error)
  1617. goto cleanup;
  1618. goto inserted;
  1619. } else {
  1620. int offset = (char *)s->here - bs->bh->b_data;
  1621. unlock_buffer(bs->bh);
  1622. ea_bdebug(bs->bh, "cloning");
  1623. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  1624. error = -ENOMEM;
  1625. if (s->base == NULL)
  1626. goto cleanup;
  1627. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  1628. s->first = ENTRY(header(s->base)+1);
  1629. header(s->base)->h_refcount = cpu_to_le32(1);
  1630. s->here = ENTRY(s->base + offset);
  1631. s->end = s->base + bs->bh->b_size;
  1632. /*
  1633. * If existing entry points to an xattr inode, we need
  1634. * to prevent ext4_xattr_set_entry() from decrementing
  1635. * ref count on it because the reference belongs to the
  1636. * original block. In this case, make the entry look
  1637. * like it has an empty value.
  1638. */
  1639. if (!s->not_found && s->here->e_value_inum) {
  1640. /*
  1641. * Defer quota free call for previous inode
  1642. * until success is guaranteed.
  1643. */
  1644. old_ea_inode_size = le32_to_cpu(
  1645. s->here->e_value_size);
  1646. s->here->e_value_inum = 0;
  1647. s->here->e_value_size = 0;
  1648. }
  1649. }
  1650. } else {
  1651. /* Allocate a buffer where we construct the new block. */
  1652. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  1653. /* assert(header == s->base) */
  1654. error = -ENOMEM;
  1655. if (s->base == NULL)
  1656. goto cleanup;
  1657. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1658. header(s->base)->h_blocks = cpu_to_le32(1);
  1659. header(s->base)->h_refcount = cpu_to_le32(1);
  1660. s->first = ENTRY(header(s->base)+1);
  1661. s->here = ENTRY(header(s->base)+1);
  1662. s->end = s->base + sb->s_blocksize;
  1663. }
  1664. error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
  1665. if (error == -EFSCORRUPTED)
  1666. goto bad_block;
  1667. if (error)
  1668. goto cleanup;
  1669. if (i->value && s->here->e_value_inum) {
  1670. unsigned int ea_ino;
  1671. /*
  1672. * A ref count on ea_inode has been taken as part of the call to
  1673. * ext4_xattr_set_entry() above. We would like to drop this
  1674. * extra ref but we have to wait until the xattr block is
  1675. * initialized and has its own ref count on the ea_inode.
  1676. */
  1677. ea_ino = le32_to_cpu(s->here->e_value_inum);
  1678. error = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
  1679. if (error) {
  1680. ea_inode = NULL;
  1681. goto cleanup;
  1682. }
  1683. }
  1684. inserted:
  1685. if (!IS_LAST_ENTRY(s->first)) {
  1686. new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
  1687. &ce);
  1688. if (new_bh) {
  1689. /* We found an identical block in the cache. */
  1690. if (new_bh == bs->bh)
  1691. ea_bdebug(new_bh, "keeping");
  1692. else {
  1693. u32 ref;
  1694. WARN_ON_ONCE(dquot_initialize_needed(inode));
  1695. /* The old block is released after updating
  1696. the inode. */
  1697. error = dquot_alloc_block(inode,
  1698. EXT4_C2B(EXT4_SB(sb), 1));
  1699. if (error)
  1700. goto cleanup;
  1701. BUFFER_TRACE(new_bh, "get_write_access");
  1702. error = ext4_journal_get_write_access(handle,
  1703. new_bh);
  1704. if (error)
  1705. goto cleanup_dquot;
  1706. lock_buffer(new_bh);
  1707. /*
  1708. * We have to be careful about races with
  1709. * freeing, rehashing or adding references to
  1710. * xattr block. Once we hold buffer lock xattr
  1711. * block's state is stable so we can check
  1712. * whether the block got freed / rehashed or
  1713. * not. Since we unhash mbcache entry under
  1714. * buffer lock when freeing / rehashing xattr
  1715. * block, checking whether entry is still
  1716. * hashed is reliable. Same rules hold for
  1717. * e_reusable handling.
  1718. */
  1719. if (hlist_bl_unhashed(&ce->e_hash_list) ||
  1720. !ce->e_reusable) {
  1721. /*
  1722. * Undo everything and check mbcache
  1723. * again.
  1724. */
  1725. unlock_buffer(new_bh);
  1726. dquot_free_block(inode,
  1727. EXT4_C2B(EXT4_SB(sb),
  1728. 1));
  1729. brelse(new_bh);
  1730. mb_cache_entry_put(ea_block_cache, ce);
  1731. ce = NULL;
  1732. new_bh = NULL;
  1733. goto inserted;
  1734. }
  1735. ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
  1736. BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
  1737. if (ref >= EXT4_XATTR_REFCOUNT_MAX)
  1738. ce->e_reusable = 0;
  1739. ea_bdebug(new_bh, "reusing; refcount now=%d",
  1740. ref);
  1741. ext4_xattr_block_csum_set(inode, new_bh);
  1742. unlock_buffer(new_bh);
  1743. error = ext4_handle_dirty_metadata(handle,
  1744. inode,
  1745. new_bh);
  1746. if (error)
  1747. goto cleanup_dquot;
  1748. }
  1749. mb_cache_entry_touch(ea_block_cache, ce);
  1750. mb_cache_entry_put(ea_block_cache, ce);
  1751. ce = NULL;
  1752. } else if (bs->bh && s->base == bs->bh->b_data) {
  1753. /* We were modifying this block in-place. */
  1754. ea_bdebug(bs->bh, "keeping this block");
  1755. ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
  1756. new_bh = bs->bh;
  1757. get_bh(new_bh);
  1758. } else {
  1759. /* We need to allocate a new block */
  1760. ext4_fsblk_t goal, block;
  1761. WARN_ON_ONCE(dquot_initialize_needed(inode));
  1762. goal = ext4_group_first_block_no(sb,
  1763. EXT4_I(inode)->i_block_group);
  1764. /* non-extent files can't have physical blocks past 2^32 */
  1765. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  1766. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  1767. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  1768. NULL, &error);
  1769. if (error)
  1770. goto cleanup;
  1771. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  1772. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  1773. ea_idebug(inode, "creating block %llu",
  1774. (unsigned long long)block);
  1775. new_bh = sb_getblk(sb, block);
  1776. if (unlikely(!new_bh)) {
  1777. error = -ENOMEM;
  1778. getblk_failed:
  1779. ext4_free_blocks(handle, inode, NULL, block, 1,
  1780. EXT4_FREE_BLOCKS_METADATA);
  1781. goto cleanup;
  1782. }
  1783. error = ext4_xattr_inode_inc_ref_all(handle, inode,
  1784. ENTRY(header(s->base)+1));
  1785. if (error)
  1786. goto getblk_failed;
  1787. if (ea_inode) {
  1788. /* Drop the extra ref on ea_inode. */
  1789. error = ext4_xattr_inode_dec_ref(handle,
  1790. ea_inode);
  1791. if (error)
  1792. ext4_warning_inode(ea_inode,
  1793. "dec ref error=%d",
  1794. error);
  1795. iput(ea_inode);
  1796. ea_inode = NULL;
  1797. }
  1798. lock_buffer(new_bh);
  1799. error = ext4_journal_get_create_access(handle, new_bh);
  1800. if (error) {
  1801. unlock_buffer(new_bh);
  1802. error = -EIO;
  1803. goto getblk_failed;
  1804. }
  1805. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  1806. ext4_xattr_block_csum_set(inode, new_bh);
  1807. set_buffer_uptodate(new_bh);
  1808. unlock_buffer(new_bh);
  1809. ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
  1810. error = ext4_handle_dirty_metadata(handle, inode,
  1811. new_bh);
  1812. if (error)
  1813. goto cleanup;
  1814. }
  1815. }
  1816. if (old_ea_inode_size)
  1817. ext4_xattr_inode_free_quota(inode, old_ea_inode_size);
  1818. /* Update the inode. */
  1819. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  1820. /* Drop the previous xattr block. */
  1821. if (bs->bh && bs->bh != new_bh) {
  1822. struct ext4_xattr_inode_array *ea_inode_array = NULL;
  1823. ext4_xattr_release_block(handle, inode, bs->bh,
  1824. &ea_inode_array,
  1825. 0 /* extra_credits */);
  1826. ext4_xattr_inode_array_free(ea_inode_array);
  1827. }
  1828. error = 0;
  1829. cleanup:
  1830. if (ea_inode) {
  1831. int error2;
  1832. error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
  1833. if (error2)
  1834. ext4_warning_inode(ea_inode, "dec ref error=%d",
  1835. error2);
  1836. /* If there was an error, revert the quota charge. */
  1837. if (error)
  1838. ext4_xattr_inode_free_quota(inode,
  1839. i_size_read(ea_inode));
  1840. iput(ea_inode);
  1841. }
  1842. if (ce)
  1843. mb_cache_entry_put(ea_block_cache, ce);
  1844. brelse(new_bh);
  1845. if (!(bs->bh && s->base == bs->bh->b_data))
  1846. kfree(s->base);
  1847. return error;
  1848. cleanup_dquot:
  1849. dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
  1850. goto cleanup;
  1851. bad_block:
  1852. EXT4_ERROR_INODE(inode, "bad block %llu",
  1853. EXT4_I(inode)->i_file_acl);
  1854. goto cleanup;
  1855. #undef header
  1856. }
  1857. int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  1858. struct ext4_xattr_ibody_find *is)
  1859. {
  1860. struct ext4_xattr_ibody_header *header;
  1861. struct ext4_inode *raw_inode;
  1862. int error;
  1863. if (EXT4_I(inode)->i_extra_isize == 0)
  1864. return 0;
  1865. raw_inode = ext4_raw_inode(&is->iloc);
  1866. header = IHDR(inode, raw_inode);
  1867. is->s.base = is->s.first = IFIRST(header);
  1868. is->s.here = is->s.first;
  1869. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1870. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  1871. error = xattr_check_inode(inode, header, is->s.end);
  1872. if (error)
  1873. return error;
  1874. /* Find the named attribute. */
  1875. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  1876. i->name, 0);
  1877. if (error && error != -ENODATA)
  1878. return error;
  1879. is->s.not_found = error;
  1880. }
  1881. return 0;
  1882. }
  1883. int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
  1884. struct ext4_xattr_info *i,
  1885. struct ext4_xattr_ibody_find *is)
  1886. {
  1887. struct ext4_xattr_ibody_header *header;
  1888. struct ext4_xattr_search *s = &is->s;
  1889. int error;
  1890. if (EXT4_I(inode)->i_extra_isize == 0)
  1891. return -ENOSPC;
  1892. error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
  1893. if (error) {
  1894. if (error == -ENOSPC &&
  1895. ext4_has_inline_data(inode)) {
  1896. error = ext4_try_to_evict_inline_data(handle, inode,
  1897. EXT4_XATTR_LEN(strlen(i->name) +
  1898. EXT4_XATTR_SIZE(i->value_len)));
  1899. if (error)
  1900. return error;
  1901. error = ext4_xattr_ibody_find(inode, i, is);
  1902. if (error)
  1903. return error;
  1904. error = ext4_xattr_set_entry(i, s, handle, inode,
  1905. false /* is_block */);
  1906. }
  1907. if (error)
  1908. return error;
  1909. }
  1910. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  1911. if (!IS_LAST_ENTRY(s->first)) {
  1912. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1913. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  1914. } else {
  1915. header->h_magic = cpu_to_le32(0);
  1916. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  1917. }
  1918. return 0;
  1919. }
  1920. static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  1921. struct ext4_xattr_info *i,
  1922. struct ext4_xattr_ibody_find *is)
  1923. {
  1924. struct ext4_xattr_ibody_header *header;
  1925. struct ext4_xattr_search *s = &is->s;
  1926. int error;
  1927. if (EXT4_I(inode)->i_extra_isize == 0)
  1928. return -ENOSPC;
  1929. error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
  1930. if (error)
  1931. return error;
  1932. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  1933. if (!IS_LAST_ENTRY(s->first)) {
  1934. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1935. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  1936. } else {
  1937. header->h_magic = cpu_to_le32(0);
  1938. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  1939. }
  1940. return 0;
  1941. }
  1942. static int ext4_xattr_value_same(struct ext4_xattr_search *s,
  1943. struct ext4_xattr_info *i)
  1944. {
  1945. void *value;
  1946. /* When e_value_inum is set the value is stored externally. */
  1947. if (s->here->e_value_inum)
  1948. return 0;
  1949. if (le32_to_cpu(s->here->e_value_size) != i->value_len)
  1950. return 0;
  1951. value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
  1952. return !memcmp(value, i->value, i->value_len);
  1953. }
  1954. static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
  1955. {
  1956. struct buffer_head *bh;
  1957. int error;
  1958. if (!EXT4_I(inode)->i_file_acl)
  1959. return NULL;
  1960. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1961. if (!bh)
  1962. return ERR_PTR(-EIO);
  1963. error = ext4_xattr_check_block(inode, bh);
  1964. if (error)
  1965. return ERR_PTR(error);
  1966. return bh;
  1967. }
  1968. /*
  1969. * ext4_xattr_set_handle()
  1970. *
  1971. * Create, replace or remove an extended attribute for this inode. Value
  1972. * is NULL to remove an existing extended attribute, and non-NULL to
  1973. * either replace an existing extended attribute, or create a new extended
  1974. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  1975. * specify that an extended attribute must exist and must not exist
  1976. * previous to the call, respectively.
  1977. *
  1978. * Returns 0, or a negative error number on failure.
  1979. */
  1980. int
  1981. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  1982. const char *name, const void *value, size_t value_len,
  1983. int flags)
  1984. {
  1985. struct ext4_xattr_info i = {
  1986. .name_index = name_index,
  1987. .name = name,
  1988. .value = value,
  1989. .value_len = value_len,
  1990. .in_inode = 0,
  1991. };
  1992. struct ext4_xattr_ibody_find is = {
  1993. .s = { .not_found = -ENODATA, },
  1994. };
  1995. struct ext4_xattr_block_find bs = {
  1996. .s = { .not_found = -ENODATA, },
  1997. };
  1998. int no_expand;
  1999. int error;
  2000. if (!name)
  2001. return -EINVAL;
  2002. if (strlen(name) > 255)
  2003. return -ERANGE;
  2004. ext4_write_lock_xattr(inode, &no_expand);
  2005. /* Check journal credits under write lock. */
  2006. if (ext4_handle_valid(handle)) {
  2007. struct buffer_head *bh;
  2008. int credits;
  2009. bh = ext4_xattr_get_block(inode);
  2010. if (IS_ERR(bh)) {
  2011. error = PTR_ERR(bh);
  2012. goto cleanup;
  2013. }
  2014. credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
  2015. value_len,
  2016. flags & XATTR_CREATE);
  2017. brelse(bh);
  2018. if (!ext4_handle_has_enough_credits(handle, credits)) {
  2019. error = -ENOSPC;
  2020. goto cleanup;
  2021. }
  2022. }
  2023. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  2024. if (error)
  2025. goto cleanup;
  2026. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  2027. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  2028. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  2029. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  2030. }
  2031. error = ext4_xattr_ibody_find(inode, &i, &is);
  2032. if (error)
  2033. goto cleanup;
  2034. if (is.s.not_found)
  2035. error = ext4_xattr_block_find(inode, &i, &bs);
  2036. if (error)
  2037. goto cleanup;
  2038. if (is.s.not_found && bs.s.not_found) {
  2039. error = -ENODATA;
  2040. if (flags & XATTR_REPLACE)
  2041. goto cleanup;
  2042. error = 0;
  2043. if (!value)
  2044. goto cleanup;
  2045. } else {
  2046. error = -EEXIST;
  2047. if (flags & XATTR_CREATE)
  2048. goto cleanup;
  2049. }
  2050. if (!value) {
  2051. if (!is.s.not_found)
  2052. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  2053. else if (!bs.s.not_found)
  2054. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  2055. } else {
  2056. error = 0;
  2057. /* Xattr value did not change? Save us some work and bail out */
  2058. if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
  2059. goto cleanup;
  2060. if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
  2061. goto cleanup;
  2062. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  2063. (EXT4_XATTR_SIZE(i.value_len) >
  2064. EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
  2065. i.in_inode = 1;
  2066. retry_inode:
  2067. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  2068. if (!error && !bs.s.not_found) {
  2069. i.value = NULL;
  2070. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  2071. } else if (error == -ENOSPC) {
  2072. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  2073. error = ext4_xattr_block_find(inode, &i, &bs);
  2074. if (error)
  2075. goto cleanup;
  2076. }
  2077. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  2078. if (!error && !is.s.not_found) {
  2079. i.value = NULL;
  2080. error = ext4_xattr_ibody_set(handle, inode, &i,
  2081. &is);
  2082. } else if (error == -ENOSPC) {
  2083. /*
  2084. * Xattr does not fit in the block, store at
  2085. * external inode if possible.
  2086. */
  2087. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  2088. !i.in_inode) {
  2089. i.in_inode = 1;
  2090. goto retry_inode;
  2091. }
  2092. }
  2093. }
  2094. }
  2095. if (!error) {
  2096. ext4_xattr_update_super_block(handle, inode->i_sb);
  2097. inode->i_ctime = current_time(inode);
  2098. if (!value)
  2099. no_expand = 0;
  2100. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  2101. /*
  2102. * The bh is consumed by ext4_mark_iloc_dirty, even with
  2103. * error != 0.
  2104. */
  2105. is.iloc.bh = NULL;
  2106. if (IS_SYNC(inode))
  2107. ext4_handle_sync(handle);
  2108. }
  2109. cleanup:
  2110. brelse(is.iloc.bh);
  2111. brelse(bs.bh);
  2112. ext4_write_unlock_xattr(inode, &no_expand);
  2113. return error;
  2114. }
  2115. int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
  2116. bool is_create, int *credits)
  2117. {
  2118. struct buffer_head *bh;
  2119. int err;
  2120. *credits = 0;
  2121. if (!EXT4_SB(inode->i_sb)->s_journal)
  2122. return 0;
  2123. down_read(&EXT4_I(inode)->xattr_sem);
  2124. bh = ext4_xattr_get_block(inode);
  2125. if (IS_ERR(bh)) {
  2126. err = PTR_ERR(bh);
  2127. } else {
  2128. *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
  2129. value_len, is_create);
  2130. brelse(bh);
  2131. err = 0;
  2132. }
  2133. up_read(&EXT4_I(inode)->xattr_sem);
  2134. return err;
  2135. }
  2136. /*
  2137. * ext4_xattr_set()
  2138. *
  2139. * Like ext4_xattr_set_handle, but start from an inode. This extended
  2140. * attribute modification is a filesystem transaction by itself.
  2141. *
  2142. * Returns 0, or a negative error number on failure.
  2143. */
  2144. int
  2145. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  2146. const void *value, size_t value_len, int flags)
  2147. {
  2148. handle_t *handle;
  2149. struct super_block *sb = inode->i_sb;
  2150. int error, retries = 0;
  2151. int credits;
  2152. error = dquot_initialize(inode);
  2153. if (error)
  2154. return error;
  2155. retry:
  2156. error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
  2157. &credits);
  2158. if (error)
  2159. return error;
  2160. handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
  2161. if (IS_ERR(handle)) {
  2162. error = PTR_ERR(handle);
  2163. } else {
  2164. int error2;
  2165. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  2166. value, value_len, flags);
  2167. error2 = ext4_journal_stop(handle);
  2168. if (error == -ENOSPC &&
  2169. ext4_should_retry_alloc(sb, &retries))
  2170. goto retry;
  2171. if (error == 0)
  2172. error = error2;
  2173. }
  2174. return error;
  2175. }
  2176. /*
  2177. * Shift the EA entries in the inode to create space for the increased
  2178. * i_extra_isize.
  2179. */
  2180. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  2181. int value_offs_shift, void *to,
  2182. void *from, size_t n)
  2183. {
  2184. struct ext4_xattr_entry *last = entry;
  2185. int new_offs;
  2186. /* We always shift xattr headers further thus offsets get lower */
  2187. BUG_ON(value_offs_shift > 0);
  2188. /* Adjust the value offsets of the entries */
  2189. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  2190. if (!last->e_value_inum && last->e_value_size) {
  2191. new_offs = le16_to_cpu(last->e_value_offs) +
  2192. value_offs_shift;
  2193. last->e_value_offs = cpu_to_le16(new_offs);
  2194. }
  2195. }
  2196. /* Shift the entries by n bytes */
  2197. memmove(to, from, n);
  2198. }
  2199. /*
  2200. * Move xattr pointed to by 'entry' from inode into external xattr block
  2201. */
  2202. static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
  2203. struct ext4_inode *raw_inode,
  2204. struct ext4_xattr_entry *entry)
  2205. {
  2206. struct ext4_xattr_ibody_find *is = NULL;
  2207. struct ext4_xattr_block_find *bs = NULL;
  2208. char *buffer = NULL, *b_entry_name = NULL;
  2209. size_t value_size = le32_to_cpu(entry->e_value_size);
  2210. struct ext4_xattr_info i = {
  2211. .value = NULL,
  2212. .value_len = 0,
  2213. .name_index = entry->e_name_index,
  2214. .in_inode = !!entry->e_value_inum,
  2215. };
  2216. struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
  2217. int error;
  2218. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  2219. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  2220. buffer = kmalloc(value_size, GFP_NOFS);
  2221. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  2222. if (!is || !bs || !buffer || !b_entry_name) {
  2223. error = -ENOMEM;
  2224. goto out;
  2225. }
  2226. is->s.not_found = -ENODATA;
  2227. bs->s.not_found = -ENODATA;
  2228. is->iloc.bh = NULL;
  2229. bs->bh = NULL;
  2230. /* Save the entry name and the entry value */
  2231. if (entry->e_value_inum) {
  2232. error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
  2233. if (error)
  2234. goto out;
  2235. } else {
  2236. size_t value_offs = le16_to_cpu(entry->e_value_offs);
  2237. memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
  2238. }
  2239. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  2240. b_entry_name[entry->e_name_len] = '\0';
  2241. i.name = b_entry_name;
  2242. error = ext4_get_inode_loc(inode, &is->iloc);
  2243. if (error)
  2244. goto out;
  2245. error = ext4_xattr_ibody_find(inode, &i, is);
  2246. if (error)
  2247. goto out;
  2248. /* Remove the chosen entry from the inode */
  2249. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  2250. if (error)
  2251. goto out;
  2252. i.value = buffer;
  2253. i.value_len = value_size;
  2254. error = ext4_xattr_block_find(inode, &i, bs);
  2255. if (error)
  2256. goto out;
  2257. /* Add entry which was removed from the inode into the block */
  2258. error = ext4_xattr_block_set(handle, inode, &i, bs);
  2259. if (error)
  2260. goto out;
  2261. error = 0;
  2262. out:
  2263. kfree(b_entry_name);
  2264. kfree(buffer);
  2265. if (is)
  2266. brelse(is->iloc.bh);
  2267. kfree(is);
  2268. kfree(bs);
  2269. return error;
  2270. }
  2271. static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
  2272. struct ext4_inode *raw_inode,
  2273. int isize_diff, size_t ifree,
  2274. size_t bfree, int *total_ino)
  2275. {
  2276. struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
  2277. struct ext4_xattr_entry *small_entry;
  2278. struct ext4_xattr_entry *entry;
  2279. struct ext4_xattr_entry *last;
  2280. unsigned int entry_size; /* EA entry size */
  2281. unsigned int total_size; /* EA entry size + value size */
  2282. unsigned int min_total_size;
  2283. int error;
  2284. while (isize_diff > ifree) {
  2285. entry = NULL;
  2286. small_entry = NULL;
  2287. min_total_size = ~0U;
  2288. last = IFIRST(header);
  2289. /* Find the entry best suited to be pushed into EA block */
  2290. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  2291. total_size = EXT4_XATTR_LEN(last->e_name_len);
  2292. if (!last->e_value_inum)
  2293. total_size += EXT4_XATTR_SIZE(
  2294. le32_to_cpu(last->e_value_size));
  2295. if (total_size <= bfree &&
  2296. total_size < min_total_size) {
  2297. if (total_size + ifree < isize_diff) {
  2298. small_entry = last;
  2299. } else {
  2300. entry = last;
  2301. min_total_size = total_size;
  2302. }
  2303. }
  2304. }
  2305. if (entry == NULL) {
  2306. if (small_entry == NULL)
  2307. return -ENOSPC;
  2308. entry = small_entry;
  2309. }
  2310. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  2311. total_size = entry_size;
  2312. if (!entry->e_value_inum)
  2313. total_size += EXT4_XATTR_SIZE(
  2314. le32_to_cpu(entry->e_value_size));
  2315. error = ext4_xattr_move_to_block(handle, inode, raw_inode,
  2316. entry);
  2317. if (error)
  2318. return error;
  2319. *total_ino -= entry_size;
  2320. ifree += total_size;
  2321. bfree -= total_size;
  2322. }
  2323. return 0;
  2324. }
  2325. /*
  2326. * Expand an inode by new_extra_isize bytes when EAs are present.
  2327. * Returns 0 on success or negative error number on failure.
  2328. */
  2329. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  2330. struct ext4_inode *raw_inode, handle_t *handle)
  2331. {
  2332. struct ext4_xattr_ibody_header *header;
  2333. struct buffer_head *bh;
  2334. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2335. static unsigned int mnt_count;
  2336. size_t min_offs;
  2337. size_t ifree, bfree;
  2338. int total_ino;
  2339. void *base, *end;
  2340. int error = 0, tried_min_extra_isize = 0;
  2341. int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
  2342. int isize_diff; /* How much do we need to grow i_extra_isize */
  2343. retry:
  2344. isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
  2345. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  2346. return 0;
  2347. header = IHDR(inode, raw_inode);
  2348. /*
  2349. * Check if enough free space is available in the inode to shift the
  2350. * entries ahead by new_extra_isize.
  2351. */
  2352. base = IFIRST(header);
  2353. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  2354. min_offs = end - base;
  2355. total_ino = sizeof(struct ext4_xattr_ibody_header);
  2356. error = xattr_check_inode(inode, header, end);
  2357. if (error)
  2358. goto cleanup;
  2359. ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
  2360. if (ifree >= isize_diff)
  2361. goto shift;
  2362. /*
  2363. * Enough free space isn't available in the inode, check if
  2364. * EA block can hold new_extra_isize bytes.
  2365. */
  2366. if (EXT4_I(inode)->i_file_acl) {
  2367. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  2368. error = -EIO;
  2369. if (!bh)
  2370. goto cleanup;
  2371. if (ext4_xattr_check_block(inode, bh)) {
  2372. EXT4_ERROR_INODE(inode, "bad block %llu",
  2373. EXT4_I(inode)->i_file_acl);
  2374. error = -EFSCORRUPTED;
  2375. brelse(bh);
  2376. goto cleanup;
  2377. }
  2378. base = BHDR(bh);
  2379. end = bh->b_data + bh->b_size;
  2380. min_offs = end - base;
  2381. bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
  2382. NULL);
  2383. brelse(bh);
  2384. if (bfree + ifree < isize_diff) {
  2385. if (!tried_min_extra_isize && s_min_extra_isize) {
  2386. tried_min_extra_isize++;
  2387. new_extra_isize = s_min_extra_isize;
  2388. goto retry;
  2389. }
  2390. error = -ENOSPC;
  2391. goto cleanup;
  2392. }
  2393. } else {
  2394. bfree = inode->i_sb->s_blocksize;
  2395. }
  2396. error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
  2397. isize_diff, ifree, bfree,
  2398. &total_ino);
  2399. if (error) {
  2400. if (error == -ENOSPC && !tried_min_extra_isize &&
  2401. s_min_extra_isize) {
  2402. tried_min_extra_isize++;
  2403. new_extra_isize = s_min_extra_isize;
  2404. goto retry;
  2405. }
  2406. goto cleanup;
  2407. }
  2408. shift:
  2409. /* Adjust the offsets and shift the remaining entries ahead */
  2410. ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
  2411. - new_extra_isize, (void *)raw_inode +
  2412. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  2413. (void *)header, total_ino);
  2414. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  2415. cleanup:
  2416. if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
  2417. ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
  2418. inode->i_ino);
  2419. mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
  2420. }
  2421. return error;
  2422. }
  2423. #define EIA_INCR 16 /* must be 2^n */
  2424. #define EIA_MASK (EIA_INCR - 1)
  2425. /* Add the large xattr @inode into @ea_inode_array for deferred iput().
  2426. * If @ea_inode_array is new or full it will be grown and the old
  2427. * contents copied over.
  2428. */
  2429. static int
  2430. ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
  2431. struct inode *inode)
  2432. {
  2433. if (*ea_inode_array == NULL) {
  2434. /*
  2435. * Start with 15 inodes, so it fits into a power-of-two size.
  2436. * If *ea_inode_array is NULL, this is essentially offsetof()
  2437. */
  2438. (*ea_inode_array) =
  2439. kmalloc(offsetof(struct ext4_xattr_inode_array,
  2440. inodes[EIA_MASK]),
  2441. GFP_NOFS);
  2442. if (*ea_inode_array == NULL)
  2443. return -ENOMEM;
  2444. (*ea_inode_array)->count = 0;
  2445. } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
  2446. /* expand the array once all 15 + n * 16 slots are full */
  2447. struct ext4_xattr_inode_array *new_array = NULL;
  2448. int count = (*ea_inode_array)->count;
  2449. /* if new_array is NULL, this is essentially offsetof() */
  2450. new_array = kmalloc(
  2451. offsetof(struct ext4_xattr_inode_array,
  2452. inodes[count + EIA_INCR]),
  2453. GFP_NOFS);
  2454. if (new_array == NULL)
  2455. return -ENOMEM;
  2456. memcpy(new_array, *ea_inode_array,
  2457. offsetof(struct ext4_xattr_inode_array, inodes[count]));
  2458. kfree(*ea_inode_array);
  2459. *ea_inode_array = new_array;
  2460. }
  2461. (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
  2462. return 0;
  2463. }
  2464. /*
  2465. * ext4_xattr_delete_inode()
  2466. *
  2467. * Free extended attribute resources associated with this inode. Traverse
  2468. * all entries and decrement reference on any xattr inodes associated with this
  2469. * inode. This is called immediately before an inode is freed. We have exclusive
  2470. * access to the inode. If an orphan inode is deleted it will also release its
  2471. * references on xattr block and xattr inodes.
  2472. */
  2473. int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
  2474. struct ext4_xattr_inode_array **ea_inode_array,
  2475. int extra_credits)
  2476. {
  2477. struct buffer_head *bh = NULL;
  2478. struct ext4_xattr_ibody_header *header;
  2479. struct ext4_iloc iloc = { .bh = NULL };
  2480. struct ext4_xattr_entry *entry;
  2481. int error;
  2482. error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
  2483. NULL /* bh */,
  2484. false /* dirty */,
  2485. false /* block_csum */);
  2486. if (error) {
  2487. EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
  2488. goto cleanup;
  2489. }
  2490. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  2491. ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  2492. error = ext4_get_inode_loc(inode, &iloc);
  2493. if (error) {
  2494. EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
  2495. goto cleanup;
  2496. }
  2497. error = ext4_journal_get_write_access(handle, iloc.bh);
  2498. if (error) {
  2499. EXT4_ERROR_INODE(inode, "write access (error %d)",
  2500. error);
  2501. goto cleanup;
  2502. }
  2503. header = IHDR(inode, ext4_raw_inode(&iloc));
  2504. if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
  2505. ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
  2506. IFIRST(header),
  2507. false /* block_csum */,
  2508. ea_inode_array,
  2509. extra_credits,
  2510. false /* skip_quota */);
  2511. }
  2512. if (EXT4_I(inode)->i_file_acl) {
  2513. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  2514. if (!bh) {
  2515. EXT4_ERROR_INODE(inode, "block %llu read error",
  2516. EXT4_I(inode)->i_file_acl);
  2517. error = -EIO;
  2518. goto cleanup;
  2519. }
  2520. error = ext4_xattr_check_block(inode, bh);
  2521. if (error) {
  2522. EXT4_ERROR_INODE(inode, "bad block %llu (error %d)",
  2523. EXT4_I(inode)->i_file_acl, error);
  2524. goto cleanup;
  2525. }
  2526. if (ext4_has_feature_ea_inode(inode->i_sb)) {
  2527. for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
  2528. entry = EXT4_XATTR_NEXT(entry))
  2529. if (entry->e_value_inum)
  2530. ext4_xattr_inode_free_quota(inode,
  2531. le32_to_cpu(entry->e_value_size));
  2532. }
  2533. ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
  2534. extra_credits);
  2535. /*
  2536. * Update i_file_acl value in the same transaction that releases
  2537. * block.
  2538. */
  2539. EXT4_I(inode)->i_file_acl = 0;
  2540. error = ext4_mark_inode_dirty(handle, inode);
  2541. if (error) {
  2542. EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
  2543. error);
  2544. goto cleanup;
  2545. }
  2546. }
  2547. error = 0;
  2548. cleanup:
  2549. brelse(iloc.bh);
  2550. brelse(bh);
  2551. return error;
  2552. }
  2553. void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
  2554. {
  2555. int idx;
  2556. if (ea_inode_array == NULL)
  2557. return;
  2558. for (idx = 0; idx < ea_inode_array->count; ++idx)
  2559. iput(ea_inode_array->inodes[idx]);
  2560. kfree(ea_inode_array);
  2561. }
  2562. /*
  2563. * ext4_xattr_block_cache_insert()
  2564. *
  2565. * Create a new entry in the extended attribute block cache, and insert
  2566. * it unless such an entry is already in the cache.
  2567. *
  2568. * Returns 0, or a negative error number on failure.
  2569. */
  2570. static void
  2571. ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
  2572. struct buffer_head *bh)
  2573. {
  2574. struct ext4_xattr_header *header = BHDR(bh);
  2575. __u32 hash = le32_to_cpu(header->h_hash);
  2576. int reusable = le32_to_cpu(header->h_refcount) <
  2577. EXT4_XATTR_REFCOUNT_MAX;
  2578. int error;
  2579. if (!ea_block_cache)
  2580. return;
  2581. error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
  2582. bh->b_blocknr, reusable);
  2583. if (error) {
  2584. if (error == -EBUSY)
  2585. ea_bdebug(bh, "already in cache");
  2586. } else
  2587. ea_bdebug(bh, "inserting [%x]", (int)hash);
  2588. }
  2589. /*
  2590. * ext4_xattr_cmp()
  2591. *
  2592. * Compare two extended attribute blocks for equality.
  2593. *
  2594. * Returns 0 if the blocks are equal, 1 if they differ, and
  2595. * a negative error number on errors.
  2596. */
  2597. static int
  2598. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  2599. struct ext4_xattr_header *header2)
  2600. {
  2601. struct ext4_xattr_entry *entry1, *entry2;
  2602. entry1 = ENTRY(header1+1);
  2603. entry2 = ENTRY(header2+1);
  2604. while (!IS_LAST_ENTRY(entry1)) {
  2605. if (IS_LAST_ENTRY(entry2))
  2606. return 1;
  2607. if (entry1->e_hash != entry2->e_hash ||
  2608. entry1->e_name_index != entry2->e_name_index ||
  2609. entry1->e_name_len != entry2->e_name_len ||
  2610. entry1->e_value_size != entry2->e_value_size ||
  2611. entry1->e_value_inum != entry2->e_value_inum ||
  2612. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  2613. return 1;
  2614. if (!entry1->e_value_inum &&
  2615. memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  2616. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  2617. le32_to_cpu(entry1->e_value_size)))
  2618. return 1;
  2619. entry1 = EXT4_XATTR_NEXT(entry1);
  2620. entry2 = EXT4_XATTR_NEXT(entry2);
  2621. }
  2622. if (!IS_LAST_ENTRY(entry2))
  2623. return 1;
  2624. return 0;
  2625. }
  2626. /*
  2627. * ext4_xattr_block_cache_find()
  2628. *
  2629. * Find an identical extended attribute block.
  2630. *
  2631. * Returns a pointer to the block found, or NULL if such a block was
  2632. * not found or an error occurred.
  2633. */
  2634. static struct buffer_head *
  2635. ext4_xattr_block_cache_find(struct inode *inode,
  2636. struct ext4_xattr_header *header,
  2637. struct mb_cache_entry **pce)
  2638. {
  2639. __u32 hash = le32_to_cpu(header->h_hash);
  2640. struct mb_cache_entry *ce;
  2641. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  2642. if (!ea_block_cache)
  2643. return NULL;
  2644. if (!header->h_hash)
  2645. return NULL; /* never share */
  2646. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  2647. ce = mb_cache_entry_find_first(ea_block_cache, hash);
  2648. while (ce) {
  2649. struct buffer_head *bh;
  2650. bh = sb_bread(inode->i_sb, ce->e_value);
  2651. if (!bh) {
  2652. EXT4_ERROR_INODE(inode, "block %lu read error",
  2653. (unsigned long)ce->e_value);
  2654. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  2655. *pce = ce;
  2656. return bh;
  2657. }
  2658. brelse(bh);
  2659. ce = mb_cache_entry_find_next(ea_block_cache, ce);
  2660. }
  2661. return NULL;
  2662. }
  2663. #define NAME_HASH_SHIFT 5
  2664. #define VALUE_HASH_SHIFT 16
  2665. /*
  2666. * ext4_xattr_hash_entry()
  2667. *
  2668. * Compute the hash of an extended attribute.
  2669. */
  2670. static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
  2671. size_t value_count)
  2672. {
  2673. __u32 hash = 0;
  2674. while (name_len--) {
  2675. hash = (hash << NAME_HASH_SHIFT) ^
  2676. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  2677. *name++;
  2678. }
  2679. while (value_count--) {
  2680. hash = (hash << VALUE_HASH_SHIFT) ^
  2681. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  2682. le32_to_cpu(*value++);
  2683. }
  2684. return cpu_to_le32(hash);
  2685. }
  2686. #undef NAME_HASH_SHIFT
  2687. #undef VALUE_HASH_SHIFT
  2688. #define BLOCK_HASH_SHIFT 16
  2689. /*
  2690. * ext4_xattr_rehash()
  2691. *
  2692. * Re-compute the extended attribute hash value after an entry has changed.
  2693. */
  2694. static void ext4_xattr_rehash(struct ext4_xattr_header *header)
  2695. {
  2696. struct ext4_xattr_entry *here;
  2697. __u32 hash = 0;
  2698. here = ENTRY(header+1);
  2699. while (!IS_LAST_ENTRY(here)) {
  2700. if (!here->e_hash) {
  2701. /* Block is not shared if an entry's hash value == 0 */
  2702. hash = 0;
  2703. break;
  2704. }
  2705. hash = (hash << BLOCK_HASH_SHIFT) ^
  2706. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  2707. le32_to_cpu(here->e_hash);
  2708. here = EXT4_XATTR_NEXT(here);
  2709. }
  2710. header->h_hash = cpu_to_le32(hash);
  2711. }
  2712. #undef BLOCK_HASH_SHIFT
  2713. #define HASH_BUCKET_BITS 10
  2714. struct mb_cache *
  2715. ext4_xattr_create_cache(void)
  2716. {
  2717. return mb_cache_create(HASH_BUCKET_BITS);
  2718. }
  2719. void ext4_xattr_destroy_cache(struct mb_cache *cache)
  2720. {
  2721. if (cache)
  2722. mb_cache_destroy(cache);
  2723. }