xattr.c 81 KB

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