xattr.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. /*
  2. * linux/fs/ext4/xattr.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. *
  6. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  7. * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
  8. * Extended attributes for symlinks and special files added per
  9. * suggestion of Luka Renko <luka.renko@hermes.si>.
  10. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  11. * Red Hat Inc.
  12. * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
  13. * and Andreas Gruenbacher <agruen@suse.de>.
  14. */
  15. /*
  16. * Extended attributes are stored directly in inodes (on file systems with
  17. * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
  18. * field contains the block number if an inode uses an additional block. All
  19. * attributes must fit in the inode and one additional block. Blocks that
  20. * contain the identical set of attributes may be shared among several inodes.
  21. * Identical blocks are detected by keeping a cache of blocks that have
  22. * recently been accessed.
  23. *
  24. * The attributes in inodes and on blocks have a different header; the entries
  25. * are stored in the same format:
  26. *
  27. * +------------------+
  28. * | header |
  29. * | entry 1 | |
  30. * | entry 2 | | growing downwards
  31. * | entry 3 | v
  32. * | four null bytes |
  33. * | . . . |
  34. * | value 1 | ^
  35. * | value 3 | | growing upwards
  36. * | value 2 | |
  37. * +------------------+
  38. *
  39. * The header is followed by multiple entry descriptors. In disk blocks, the
  40. * entry descriptors are kept sorted. In inodes, they are unsorted. The
  41. * attribute values are aligned to the end of the block in no specific order.
  42. *
  43. * Locking strategy
  44. * ----------------
  45. * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
  46. * EA blocks are only changed if they are exclusive to an inode, so
  47. * holding xattr_sem also means that nothing but the EA block's reference
  48. * count can change. Multiple writers to the same block are synchronized
  49. * by the buffer lock.
  50. */
  51. #include <linux/init.h>
  52. #include <linux/fs.h>
  53. #include <linux/slab.h>
  54. #include <linux/mbcache.h>
  55. #include <linux/quotaops.h>
  56. #include <linux/rwsem.h>
  57. #include "ext4_jbd2.h"
  58. #include "ext4.h"
  59. #include "xattr.h"
  60. #include "acl.h"
  61. #ifdef EXT4_XATTR_DEBUG
  62. # define ea_idebug(inode, f...) do { \
  63. printk(KERN_DEBUG "inode %s:%lu: ", \
  64. inode->i_sb->s_id, inode->i_ino); \
  65. printk(f); \
  66. printk("\n"); \
  67. } while (0)
  68. # define ea_bdebug(bh, f...) do { \
  69. char b[BDEVNAME_SIZE]; \
  70. printk(KERN_DEBUG "block %s:%lu: ", \
  71. bdevname(bh->b_bdev, b), \
  72. (unsigned long) bh->b_blocknr); \
  73. printk(f); \
  74. printk("\n"); \
  75. } while (0)
  76. #else
  77. # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  78. # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  79. #endif
  80. static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
  81. static struct buffer_head *ext4_xattr_cache_find(struct inode *,
  82. struct ext4_xattr_header *,
  83. struct mb_cache_entry **);
  84. static void ext4_xattr_rehash(struct ext4_xattr_header *,
  85. struct ext4_xattr_entry *);
  86. static int ext4_xattr_list(struct dentry *dentry, char *buffer,
  87. size_t buffer_size);
  88. static const struct xattr_handler *ext4_xattr_handler_map[] = {
  89. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  90. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  91. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  92. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  93. #endif
  94. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  95. #ifdef CONFIG_EXT4_FS_SECURITY
  96. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  97. #endif
  98. };
  99. const struct xattr_handler *ext4_xattr_handlers[] = {
  100. &ext4_xattr_user_handler,
  101. &ext4_xattr_trusted_handler,
  102. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  103. &posix_acl_access_xattr_handler,
  104. &posix_acl_default_xattr_handler,
  105. #endif
  106. #ifdef CONFIG_EXT4_FS_SECURITY
  107. &ext4_xattr_security_handler,
  108. #endif
  109. NULL
  110. };
  111. #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
  112. inode->i_sb->s_fs_info)->s_mb_cache)
  113. static __le32 ext4_xattr_block_csum(struct inode *inode,
  114. sector_t block_nr,
  115. struct ext4_xattr_header *hdr)
  116. {
  117. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  118. __u32 csum;
  119. __le32 save_csum;
  120. __le64 dsk_block_nr = cpu_to_le64(block_nr);
  121. save_csum = hdr->h_checksum;
  122. hdr->h_checksum = 0;
  123. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
  124. sizeof(dsk_block_nr));
  125. csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
  126. EXT4_BLOCK_SIZE(inode->i_sb));
  127. hdr->h_checksum = save_csum;
  128. return cpu_to_le32(csum);
  129. }
  130. static int ext4_xattr_block_csum_verify(struct inode *inode,
  131. sector_t block_nr,
  132. struct ext4_xattr_header *hdr)
  133. {
  134. if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  135. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
  136. (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
  137. return 0;
  138. return 1;
  139. }
  140. static void ext4_xattr_block_csum_set(struct inode *inode,
  141. sector_t block_nr,
  142. struct ext4_xattr_header *hdr)
  143. {
  144. if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  145. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  146. return;
  147. hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
  148. }
  149. static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
  150. struct inode *inode,
  151. struct buffer_head *bh)
  152. {
  153. ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
  154. return ext4_handle_dirty_metadata(handle, inode, bh);
  155. }
  156. static inline const struct xattr_handler *
  157. ext4_xattr_handler(int name_index)
  158. {
  159. const struct xattr_handler *handler = NULL;
  160. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  161. handler = ext4_xattr_handler_map[name_index];
  162. return handler;
  163. }
  164. /*
  165. * Inode operation listxattr()
  166. *
  167. * dentry->d_inode->i_mutex: don't care
  168. */
  169. ssize_t
  170. ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
  171. {
  172. return ext4_xattr_list(dentry, buffer, size);
  173. }
  174. static int
  175. ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
  176. {
  177. while (!IS_LAST_ENTRY(entry)) {
  178. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
  179. if ((void *)next >= end)
  180. return -EIO;
  181. entry = next;
  182. }
  183. return 0;
  184. }
  185. static inline int
  186. ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
  187. {
  188. int error;
  189. if (buffer_verified(bh))
  190. return 0;
  191. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  192. BHDR(bh)->h_blocks != cpu_to_le32(1))
  193. return -EIO;
  194. if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
  195. return -EIO;
  196. error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  197. if (!error)
  198. set_buffer_verified(bh);
  199. return error;
  200. }
  201. static inline int
  202. ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
  203. {
  204. size_t value_size = le32_to_cpu(entry->e_value_size);
  205. if (entry->e_value_block != 0 || value_size > size ||
  206. le16_to_cpu(entry->e_value_offs) + value_size > size)
  207. return -EIO;
  208. return 0;
  209. }
  210. static int
  211. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  212. const char *name, size_t size, int sorted)
  213. {
  214. struct ext4_xattr_entry *entry;
  215. size_t name_len;
  216. int cmp = 1;
  217. if (name == NULL)
  218. return -EINVAL;
  219. name_len = strlen(name);
  220. entry = *pentry;
  221. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  222. cmp = name_index - entry->e_name_index;
  223. if (!cmp)
  224. cmp = name_len - entry->e_name_len;
  225. if (!cmp)
  226. cmp = memcmp(name, entry->e_name, name_len);
  227. if (cmp <= 0 && (sorted || cmp == 0))
  228. break;
  229. }
  230. *pentry = entry;
  231. if (!cmp && ext4_xattr_check_entry(entry, size))
  232. return -EIO;
  233. return cmp ? -ENODATA : 0;
  234. }
  235. static int
  236. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  237. void *buffer, size_t buffer_size)
  238. {
  239. struct buffer_head *bh = NULL;
  240. struct ext4_xattr_entry *entry;
  241. size_t size;
  242. int error;
  243. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  244. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  245. name_index, name, buffer, (long)buffer_size);
  246. error = -ENODATA;
  247. if (!EXT4_I(inode)->i_file_acl)
  248. goto cleanup;
  249. ea_idebug(inode, "reading block %llu",
  250. (unsigned long long)EXT4_I(inode)->i_file_acl);
  251. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  252. if (!bh)
  253. goto cleanup;
  254. ea_bdebug(bh, "b_count=%d, refcount=%d",
  255. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  256. if (ext4_xattr_check_block(inode, bh)) {
  257. bad_block:
  258. EXT4_ERROR_INODE(inode, "bad block %llu",
  259. EXT4_I(inode)->i_file_acl);
  260. error = -EIO;
  261. goto cleanup;
  262. }
  263. ext4_xattr_cache_insert(ext4_mb_cache, bh);
  264. entry = BFIRST(bh);
  265. error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  266. if (error == -EIO)
  267. goto bad_block;
  268. if (error)
  269. goto cleanup;
  270. size = le32_to_cpu(entry->e_value_size);
  271. if (buffer) {
  272. error = -ERANGE;
  273. if (size > buffer_size)
  274. goto cleanup;
  275. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  276. size);
  277. }
  278. error = size;
  279. cleanup:
  280. brelse(bh);
  281. return error;
  282. }
  283. int
  284. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  285. void *buffer, size_t buffer_size)
  286. {
  287. struct ext4_xattr_ibody_header *header;
  288. struct ext4_xattr_entry *entry;
  289. struct ext4_inode *raw_inode;
  290. struct ext4_iloc iloc;
  291. size_t size;
  292. void *end;
  293. int error;
  294. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  295. return -ENODATA;
  296. error = ext4_get_inode_loc(inode, &iloc);
  297. if (error)
  298. return error;
  299. raw_inode = ext4_raw_inode(&iloc);
  300. header = IHDR(inode, raw_inode);
  301. entry = IFIRST(header);
  302. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  303. error = ext4_xattr_check_names(entry, end);
  304. if (error)
  305. goto cleanup;
  306. error = ext4_xattr_find_entry(&entry, name_index, name,
  307. end - (void *)entry, 0);
  308. if (error)
  309. goto cleanup;
  310. size = le32_to_cpu(entry->e_value_size);
  311. if (buffer) {
  312. error = -ERANGE;
  313. if (size > buffer_size)
  314. goto cleanup;
  315. memcpy(buffer, (void *)IFIRST(header) +
  316. le16_to_cpu(entry->e_value_offs), size);
  317. }
  318. error = size;
  319. cleanup:
  320. brelse(iloc.bh);
  321. return error;
  322. }
  323. /*
  324. * ext4_xattr_get()
  325. *
  326. * Copy an extended attribute into the buffer
  327. * provided, or compute the buffer size required.
  328. * Buffer is NULL to compute the size of the buffer required.
  329. *
  330. * Returns a negative error number on failure, or the number of bytes
  331. * used / required on success.
  332. */
  333. int
  334. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  335. void *buffer, size_t buffer_size)
  336. {
  337. int error;
  338. if (strlen(name) > 255)
  339. return -ERANGE;
  340. down_read(&EXT4_I(inode)->xattr_sem);
  341. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  342. buffer_size);
  343. if (error == -ENODATA)
  344. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  345. buffer_size);
  346. up_read(&EXT4_I(inode)->xattr_sem);
  347. return error;
  348. }
  349. static int
  350. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  351. char *buffer, size_t buffer_size)
  352. {
  353. size_t rest = buffer_size;
  354. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  355. const struct xattr_handler *handler =
  356. ext4_xattr_handler(entry->e_name_index);
  357. if (handler) {
  358. size_t size = handler->list(dentry, buffer, rest,
  359. entry->e_name,
  360. entry->e_name_len,
  361. handler->flags);
  362. if (buffer) {
  363. if (size > rest)
  364. return -ERANGE;
  365. buffer += size;
  366. }
  367. rest -= size;
  368. }
  369. }
  370. return buffer_size - rest;
  371. }
  372. static int
  373. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  374. {
  375. struct inode *inode = dentry->d_inode;
  376. struct buffer_head *bh = NULL;
  377. int error;
  378. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  379. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  380. buffer, (long)buffer_size);
  381. error = 0;
  382. if (!EXT4_I(inode)->i_file_acl)
  383. goto cleanup;
  384. ea_idebug(inode, "reading block %llu",
  385. (unsigned long long)EXT4_I(inode)->i_file_acl);
  386. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  387. error = -EIO;
  388. if (!bh)
  389. goto cleanup;
  390. ea_bdebug(bh, "b_count=%d, refcount=%d",
  391. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  392. if (ext4_xattr_check_block(inode, bh)) {
  393. EXT4_ERROR_INODE(inode, "bad block %llu",
  394. EXT4_I(inode)->i_file_acl);
  395. error = -EIO;
  396. goto cleanup;
  397. }
  398. ext4_xattr_cache_insert(ext4_mb_cache, bh);
  399. error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  400. cleanup:
  401. brelse(bh);
  402. return error;
  403. }
  404. static int
  405. ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  406. {
  407. struct inode *inode = dentry->d_inode;
  408. struct ext4_xattr_ibody_header *header;
  409. struct ext4_inode *raw_inode;
  410. struct ext4_iloc iloc;
  411. void *end;
  412. int error;
  413. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  414. return 0;
  415. error = ext4_get_inode_loc(inode, &iloc);
  416. if (error)
  417. return error;
  418. raw_inode = ext4_raw_inode(&iloc);
  419. header = IHDR(inode, raw_inode);
  420. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  421. error = ext4_xattr_check_names(IFIRST(header), end);
  422. if (error)
  423. goto cleanup;
  424. error = ext4_xattr_list_entries(dentry, IFIRST(header),
  425. buffer, buffer_size);
  426. cleanup:
  427. brelse(iloc.bh);
  428. return error;
  429. }
  430. /*
  431. * ext4_xattr_list()
  432. *
  433. * Copy a list of attribute names into the buffer
  434. * provided, or compute the buffer size required.
  435. * Buffer is NULL to compute the size of the buffer required.
  436. *
  437. * Returns a negative error number on failure, or the number of bytes
  438. * used / required on success.
  439. */
  440. static int
  441. ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  442. {
  443. int ret, ret2;
  444. down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
  445. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  446. if (ret < 0)
  447. goto errout;
  448. if (buffer) {
  449. buffer += ret;
  450. buffer_size -= ret;
  451. }
  452. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  453. if (ret < 0)
  454. goto errout;
  455. ret += ret2;
  456. errout:
  457. up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
  458. return ret;
  459. }
  460. /*
  461. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  462. * not set, set it.
  463. */
  464. static void ext4_xattr_update_super_block(handle_t *handle,
  465. struct super_block *sb)
  466. {
  467. if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
  468. return;
  469. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  470. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  471. EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
  472. ext4_handle_dirty_super(handle, sb);
  473. }
  474. }
  475. /*
  476. * Release the xattr block BH: If the reference count is > 1, decrement it;
  477. * otherwise free the block.
  478. */
  479. static void
  480. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  481. struct buffer_head *bh)
  482. {
  483. struct mb_cache_entry *ce = NULL;
  484. int error = 0;
  485. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  486. ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
  487. BUFFER_TRACE(bh, "get_write_access");
  488. error = ext4_journal_get_write_access(handle, bh);
  489. if (error)
  490. goto out;
  491. lock_buffer(bh);
  492. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  493. ea_bdebug(bh, "refcount now=0; freeing");
  494. if (ce)
  495. mb_cache_entry_free(ce);
  496. get_bh(bh);
  497. unlock_buffer(bh);
  498. ext4_free_blocks(handle, inode, bh, 0, 1,
  499. EXT4_FREE_BLOCKS_METADATA |
  500. EXT4_FREE_BLOCKS_FORGET);
  501. } else {
  502. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  503. if (ce)
  504. mb_cache_entry_release(ce);
  505. /*
  506. * Beware of this ugliness: Releasing of xattr block references
  507. * from different inodes can race and so we have to protect
  508. * from a race where someone else frees the block (and releases
  509. * its journal_head) before we are done dirtying the buffer. In
  510. * nojournal mode this race is harmless and we actually cannot
  511. * call ext4_handle_dirty_xattr_block() with locked buffer as
  512. * that function can call sync_dirty_buffer() so for that case
  513. * we handle the dirtying after unlocking the buffer.
  514. */
  515. if (ext4_handle_valid(handle))
  516. error = ext4_handle_dirty_xattr_block(handle, inode,
  517. bh);
  518. unlock_buffer(bh);
  519. if (!ext4_handle_valid(handle))
  520. error = ext4_handle_dirty_xattr_block(handle, inode,
  521. bh);
  522. if (IS_SYNC(inode))
  523. ext4_handle_sync(handle);
  524. dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
  525. ea_bdebug(bh, "refcount now=%d; releasing",
  526. le32_to_cpu(BHDR(bh)->h_refcount));
  527. }
  528. out:
  529. ext4_std_error(inode->i_sb, error);
  530. return;
  531. }
  532. /*
  533. * Find the available free space for EAs. This also returns the total number of
  534. * bytes used by EA entries.
  535. */
  536. static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
  537. size_t *min_offs, void *base, int *total)
  538. {
  539. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  540. if (!last->e_value_block && last->e_value_size) {
  541. size_t offs = le16_to_cpu(last->e_value_offs);
  542. if (offs < *min_offs)
  543. *min_offs = offs;
  544. }
  545. if (total)
  546. *total += EXT4_XATTR_LEN(last->e_name_len);
  547. }
  548. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  549. }
  550. static int
  551. ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
  552. {
  553. struct ext4_xattr_entry *last;
  554. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  555. /* Compute min_offs and last. */
  556. last = s->first;
  557. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  558. if (!last->e_value_block && last->e_value_size) {
  559. size_t offs = le16_to_cpu(last->e_value_offs);
  560. if (offs < min_offs)
  561. min_offs = offs;
  562. }
  563. }
  564. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  565. if (!s->not_found) {
  566. if (!s->here->e_value_block && s->here->e_value_size) {
  567. size_t size = le32_to_cpu(s->here->e_value_size);
  568. free += EXT4_XATTR_SIZE(size);
  569. }
  570. free += EXT4_XATTR_LEN(name_len);
  571. }
  572. if (i->value) {
  573. if (free < EXT4_XATTR_SIZE(i->value_len) ||
  574. free < EXT4_XATTR_LEN(name_len) +
  575. EXT4_XATTR_SIZE(i->value_len))
  576. return -ENOSPC;
  577. }
  578. if (i->value && s->not_found) {
  579. /* Insert the new name. */
  580. size_t size = EXT4_XATTR_LEN(name_len);
  581. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  582. memmove((void *)s->here + size, s->here, rest);
  583. memset(s->here, 0, size);
  584. s->here->e_name_index = i->name_index;
  585. s->here->e_name_len = name_len;
  586. memcpy(s->here->e_name, i->name, name_len);
  587. } else {
  588. if (!s->here->e_value_block && s->here->e_value_size) {
  589. void *first_val = s->base + min_offs;
  590. size_t offs = le16_to_cpu(s->here->e_value_offs);
  591. void *val = s->base + offs;
  592. size_t size = EXT4_XATTR_SIZE(
  593. le32_to_cpu(s->here->e_value_size));
  594. if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
  595. /* The old and the new value have the same
  596. size. Just replace. */
  597. s->here->e_value_size =
  598. cpu_to_le32(i->value_len);
  599. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  600. memset(val, 0, size);
  601. } else {
  602. /* Clear pad bytes first. */
  603. memset(val + size - EXT4_XATTR_PAD, 0,
  604. EXT4_XATTR_PAD);
  605. memcpy(val, i->value, i->value_len);
  606. }
  607. return 0;
  608. }
  609. /* Remove the old value. */
  610. memmove(first_val + size, first_val, val - first_val);
  611. memset(first_val, 0, size);
  612. s->here->e_value_size = 0;
  613. s->here->e_value_offs = 0;
  614. min_offs += size;
  615. /* Adjust all value offsets. */
  616. last = s->first;
  617. while (!IS_LAST_ENTRY(last)) {
  618. size_t o = le16_to_cpu(last->e_value_offs);
  619. if (!last->e_value_block &&
  620. last->e_value_size && o < offs)
  621. last->e_value_offs =
  622. cpu_to_le16(o + size);
  623. last = EXT4_XATTR_NEXT(last);
  624. }
  625. }
  626. if (!i->value) {
  627. /* Remove the old name. */
  628. size_t size = EXT4_XATTR_LEN(name_len);
  629. last = ENTRY((void *)last - size);
  630. memmove(s->here, (void *)s->here + size,
  631. (void *)last - (void *)s->here + sizeof(__u32));
  632. memset(last, 0, size);
  633. }
  634. }
  635. if (i->value) {
  636. /* Insert the new value. */
  637. s->here->e_value_size = cpu_to_le32(i->value_len);
  638. if (i->value_len) {
  639. size_t size = EXT4_XATTR_SIZE(i->value_len);
  640. void *val = s->base + min_offs - size;
  641. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  642. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  643. memset(val, 0, size);
  644. } else {
  645. /* Clear the pad bytes first. */
  646. memset(val + size - EXT4_XATTR_PAD, 0,
  647. EXT4_XATTR_PAD);
  648. memcpy(val, i->value, i->value_len);
  649. }
  650. }
  651. }
  652. return 0;
  653. }
  654. struct ext4_xattr_block_find {
  655. struct ext4_xattr_search s;
  656. struct buffer_head *bh;
  657. };
  658. static int
  659. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  660. struct ext4_xattr_block_find *bs)
  661. {
  662. struct super_block *sb = inode->i_sb;
  663. int error;
  664. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  665. i->name_index, i->name, i->value, (long)i->value_len);
  666. if (EXT4_I(inode)->i_file_acl) {
  667. /* The inode already has an extended attribute block. */
  668. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  669. error = -EIO;
  670. if (!bs->bh)
  671. goto cleanup;
  672. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  673. atomic_read(&(bs->bh->b_count)),
  674. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  675. if (ext4_xattr_check_block(inode, bs->bh)) {
  676. EXT4_ERROR_INODE(inode, "bad block %llu",
  677. EXT4_I(inode)->i_file_acl);
  678. error = -EIO;
  679. goto cleanup;
  680. }
  681. /* Find the named attribute. */
  682. bs->s.base = BHDR(bs->bh);
  683. bs->s.first = BFIRST(bs->bh);
  684. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  685. bs->s.here = bs->s.first;
  686. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  687. i->name, bs->bh->b_size, 1);
  688. if (error && error != -ENODATA)
  689. goto cleanup;
  690. bs->s.not_found = error;
  691. }
  692. error = 0;
  693. cleanup:
  694. return error;
  695. }
  696. static int
  697. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  698. struct ext4_xattr_info *i,
  699. struct ext4_xattr_block_find *bs)
  700. {
  701. struct super_block *sb = inode->i_sb;
  702. struct buffer_head *new_bh = NULL;
  703. struct ext4_xattr_search *s = &bs->s;
  704. struct mb_cache_entry *ce = NULL;
  705. int error = 0;
  706. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  707. #define header(x) ((struct ext4_xattr_header *)(x))
  708. if (i->value && i->value_len > sb->s_blocksize)
  709. return -ENOSPC;
  710. if (s->base) {
  711. ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
  712. bs->bh->b_blocknr);
  713. BUFFER_TRACE(bs->bh, "get_write_access");
  714. error = ext4_journal_get_write_access(handle, bs->bh);
  715. if (error)
  716. goto cleanup;
  717. lock_buffer(bs->bh);
  718. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  719. if (ce) {
  720. mb_cache_entry_free(ce);
  721. ce = NULL;
  722. }
  723. ea_bdebug(bs->bh, "modifying in-place");
  724. error = ext4_xattr_set_entry(i, s);
  725. if (!error) {
  726. if (!IS_LAST_ENTRY(s->first))
  727. ext4_xattr_rehash(header(s->base),
  728. s->here);
  729. ext4_xattr_cache_insert(ext4_mb_cache,
  730. bs->bh);
  731. }
  732. unlock_buffer(bs->bh);
  733. if (error == -EIO)
  734. goto bad_block;
  735. if (!error)
  736. error = ext4_handle_dirty_xattr_block(handle,
  737. inode,
  738. bs->bh);
  739. if (error)
  740. goto cleanup;
  741. goto inserted;
  742. } else {
  743. int offset = (char *)s->here - bs->bh->b_data;
  744. unlock_buffer(bs->bh);
  745. if (ce) {
  746. mb_cache_entry_release(ce);
  747. ce = NULL;
  748. }
  749. ea_bdebug(bs->bh, "cloning");
  750. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  751. error = -ENOMEM;
  752. if (s->base == NULL)
  753. goto cleanup;
  754. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  755. s->first = ENTRY(header(s->base)+1);
  756. header(s->base)->h_refcount = cpu_to_le32(1);
  757. s->here = ENTRY(s->base + offset);
  758. s->end = s->base + bs->bh->b_size;
  759. }
  760. } else {
  761. /* Allocate a buffer where we construct the new block. */
  762. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  763. /* assert(header == s->base) */
  764. error = -ENOMEM;
  765. if (s->base == NULL)
  766. goto cleanup;
  767. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  768. header(s->base)->h_blocks = cpu_to_le32(1);
  769. header(s->base)->h_refcount = cpu_to_le32(1);
  770. s->first = ENTRY(header(s->base)+1);
  771. s->here = ENTRY(header(s->base)+1);
  772. s->end = s->base + sb->s_blocksize;
  773. }
  774. error = ext4_xattr_set_entry(i, s);
  775. if (error == -EIO)
  776. goto bad_block;
  777. if (error)
  778. goto cleanup;
  779. if (!IS_LAST_ENTRY(s->first))
  780. ext4_xattr_rehash(header(s->base), s->here);
  781. inserted:
  782. if (!IS_LAST_ENTRY(s->first)) {
  783. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  784. if (new_bh) {
  785. /* We found an identical block in the cache. */
  786. if (new_bh == bs->bh)
  787. ea_bdebug(new_bh, "keeping");
  788. else {
  789. /* The old block is released after updating
  790. the inode. */
  791. error = dquot_alloc_block(inode,
  792. EXT4_C2B(EXT4_SB(sb), 1));
  793. if (error)
  794. goto cleanup;
  795. BUFFER_TRACE(new_bh, "get_write_access");
  796. error = ext4_journal_get_write_access(handle,
  797. new_bh);
  798. if (error)
  799. goto cleanup_dquot;
  800. lock_buffer(new_bh);
  801. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  802. ea_bdebug(new_bh, "reusing; refcount now=%d",
  803. le32_to_cpu(BHDR(new_bh)->h_refcount));
  804. unlock_buffer(new_bh);
  805. error = ext4_handle_dirty_xattr_block(handle,
  806. inode,
  807. new_bh);
  808. if (error)
  809. goto cleanup_dquot;
  810. }
  811. mb_cache_entry_release(ce);
  812. ce = NULL;
  813. } else if (bs->bh && s->base == bs->bh->b_data) {
  814. /* We were modifying this block in-place. */
  815. ea_bdebug(bs->bh, "keeping this block");
  816. new_bh = bs->bh;
  817. get_bh(new_bh);
  818. } else {
  819. /* We need to allocate a new block */
  820. ext4_fsblk_t goal, block;
  821. goal = ext4_group_first_block_no(sb,
  822. EXT4_I(inode)->i_block_group);
  823. /* non-extent files can't have physical blocks past 2^32 */
  824. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  825. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  826. /*
  827. * take i_data_sem because we will test
  828. * i_delalloc_reserved_flag in ext4_mb_new_blocks
  829. */
  830. down_read(&EXT4_I(inode)->i_data_sem);
  831. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  832. NULL, &error);
  833. up_read((&EXT4_I(inode)->i_data_sem));
  834. if (error)
  835. goto cleanup;
  836. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  837. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  838. ea_idebug(inode, "creating block %llu",
  839. (unsigned long long)block);
  840. new_bh = sb_getblk(sb, block);
  841. if (unlikely(!new_bh)) {
  842. error = -ENOMEM;
  843. getblk_failed:
  844. ext4_free_blocks(handle, inode, NULL, block, 1,
  845. EXT4_FREE_BLOCKS_METADATA);
  846. goto cleanup;
  847. }
  848. lock_buffer(new_bh);
  849. error = ext4_journal_get_create_access(handle, new_bh);
  850. if (error) {
  851. unlock_buffer(new_bh);
  852. error = -EIO;
  853. goto getblk_failed;
  854. }
  855. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  856. set_buffer_uptodate(new_bh);
  857. unlock_buffer(new_bh);
  858. ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
  859. error = ext4_handle_dirty_xattr_block(handle,
  860. inode, new_bh);
  861. if (error)
  862. goto cleanup;
  863. }
  864. }
  865. /* Update the inode. */
  866. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  867. /* Drop the previous xattr block. */
  868. if (bs->bh && bs->bh != new_bh)
  869. ext4_xattr_release_block(handle, inode, bs->bh);
  870. error = 0;
  871. cleanup:
  872. if (ce)
  873. mb_cache_entry_release(ce);
  874. brelse(new_bh);
  875. if (!(bs->bh && s->base == bs->bh->b_data))
  876. kfree(s->base);
  877. return error;
  878. cleanup_dquot:
  879. dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
  880. goto cleanup;
  881. bad_block:
  882. EXT4_ERROR_INODE(inode, "bad block %llu",
  883. EXT4_I(inode)->i_file_acl);
  884. goto cleanup;
  885. #undef header
  886. }
  887. int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  888. struct ext4_xattr_ibody_find *is)
  889. {
  890. struct ext4_xattr_ibody_header *header;
  891. struct ext4_inode *raw_inode;
  892. int error;
  893. if (EXT4_I(inode)->i_extra_isize == 0)
  894. return 0;
  895. raw_inode = ext4_raw_inode(&is->iloc);
  896. header = IHDR(inode, raw_inode);
  897. is->s.base = is->s.first = IFIRST(header);
  898. is->s.here = is->s.first;
  899. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  900. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  901. error = ext4_xattr_check_names(IFIRST(header), is->s.end);
  902. if (error)
  903. return error;
  904. /* Find the named attribute. */
  905. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  906. i->name, is->s.end -
  907. (void *)is->s.base, 0);
  908. if (error && error != -ENODATA)
  909. return error;
  910. is->s.not_found = error;
  911. }
  912. return 0;
  913. }
  914. int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
  915. struct ext4_xattr_info *i,
  916. struct ext4_xattr_ibody_find *is)
  917. {
  918. struct ext4_xattr_ibody_header *header;
  919. struct ext4_xattr_search *s = &is->s;
  920. int error;
  921. if (EXT4_I(inode)->i_extra_isize == 0)
  922. return -ENOSPC;
  923. error = ext4_xattr_set_entry(i, s);
  924. if (error) {
  925. if (error == -ENOSPC &&
  926. ext4_has_inline_data(inode)) {
  927. error = ext4_try_to_evict_inline_data(handle, inode,
  928. EXT4_XATTR_LEN(strlen(i->name) +
  929. EXT4_XATTR_SIZE(i->value_len)));
  930. if (error)
  931. return error;
  932. error = ext4_xattr_ibody_find(inode, i, is);
  933. if (error)
  934. return error;
  935. error = ext4_xattr_set_entry(i, s);
  936. }
  937. if (error)
  938. return error;
  939. }
  940. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  941. if (!IS_LAST_ENTRY(s->first)) {
  942. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  943. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  944. } else {
  945. header->h_magic = cpu_to_le32(0);
  946. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  947. }
  948. return 0;
  949. }
  950. static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  951. struct ext4_xattr_info *i,
  952. struct ext4_xattr_ibody_find *is)
  953. {
  954. struct ext4_xattr_ibody_header *header;
  955. struct ext4_xattr_search *s = &is->s;
  956. int error;
  957. if (EXT4_I(inode)->i_extra_isize == 0)
  958. return -ENOSPC;
  959. error = ext4_xattr_set_entry(i, s);
  960. if (error)
  961. return error;
  962. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  963. if (!IS_LAST_ENTRY(s->first)) {
  964. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  965. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  966. } else {
  967. header->h_magic = cpu_to_le32(0);
  968. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  969. }
  970. return 0;
  971. }
  972. /*
  973. * ext4_xattr_set_handle()
  974. *
  975. * Create, replace or remove an extended attribute for this inode. Value
  976. * is NULL to remove an existing extended attribute, and non-NULL to
  977. * either replace an existing extended attribute, or create a new extended
  978. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  979. * specify that an extended attribute must exist and must not exist
  980. * previous to the call, respectively.
  981. *
  982. * Returns 0, or a negative error number on failure.
  983. */
  984. int
  985. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  986. const char *name, const void *value, size_t value_len,
  987. int flags)
  988. {
  989. struct ext4_xattr_info i = {
  990. .name_index = name_index,
  991. .name = name,
  992. .value = value,
  993. .value_len = value_len,
  994. };
  995. struct ext4_xattr_ibody_find is = {
  996. .s = { .not_found = -ENODATA, },
  997. };
  998. struct ext4_xattr_block_find bs = {
  999. .s = { .not_found = -ENODATA, },
  1000. };
  1001. unsigned long no_expand;
  1002. int error;
  1003. if (!name)
  1004. return -EINVAL;
  1005. if (strlen(name) > 255)
  1006. return -ERANGE;
  1007. down_write(&EXT4_I(inode)->xattr_sem);
  1008. no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1009. ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1010. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  1011. if (error)
  1012. goto cleanup;
  1013. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  1014. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  1015. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  1016. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  1017. }
  1018. error = ext4_xattr_ibody_find(inode, &i, &is);
  1019. if (error)
  1020. goto cleanup;
  1021. if (is.s.not_found)
  1022. error = ext4_xattr_block_find(inode, &i, &bs);
  1023. if (error)
  1024. goto cleanup;
  1025. if (is.s.not_found && bs.s.not_found) {
  1026. error = -ENODATA;
  1027. if (flags & XATTR_REPLACE)
  1028. goto cleanup;
  1029. error = 0;
  1030. if (!value)
  1031. goto cleanup;
  1032. } else {
  1033. error = -EEXIST;
  1034. if (flags & XATTR_CREATE)
  1035. goto cleanup;
  1036. }
  1037. if (!value) {
  1038. if (!is.s.not_found)
  1039. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1040. else if (!bs.s.not_found)
  1041. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1042. } else {
  1043. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1044. if (!error && !bs.s.not_found) {
  1045. i.value = NULL;
  1046. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1047. } else if (error == -ENOSPC) {
  1048. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  1049. error = ext4_xattr_block_find(inode, &i, &bs);
  1050. if (error)
  1051. goto cleanup;
  1052. }
  1053. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1054. if (error)
  1055. goto cleanup;
  1056. if (!is.s.not_found) {
  1057. i.value = NULL;
  1058. error = ext4_xattr_ibody_set(handle, inode, &i,
  1059. &is);
  1060. }
  1061. }
  1062. }
  1063. if (!error) {
  1064. ext4_xattr_update_super_block(handle, inode->i_sb);
  1065. inode->i_ctime = ext4_current_time(inode);
  1066. if (!value)
  1067. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1068. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  1069. /*
  1070. * The bh is consumed by ext4_mark_iloc_dirty, even with
  1071. * error != 0.
  1072. */
  1073. is.iloc.bh = NULL;
  1074. if (IS_SYNC(inode))
  1075. ext4_handle_sync(handle);
  1076. }
  1077. cleanup:
  1078. brelse(is.iloc.bh);
  1079. brelse(bs.bh);
  1080. if (no_expand == 0)
  1081. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1082. up_write(&EXT4_I(inode)->xattr_sem);
  1083. return error;
  1084. }
  1085. /*
  1086. * ext4_xattr_set()
  1087. *
  1088. * Like ext4_xattr_set_handle, but start from an inode. This extended
  1089. * attribute modification is a filesystem transaction by itself.
  1090. *
  1091. * Returns 0, or a negative error number on failure.
  1092. */
  1093. int
  1094. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  1095. const void *value, size_t value_len, int flags)
  1096. {
  1097. handle_t *handle;
  1098. int error, retries = 0;
  1099. int credits = ext4_jbd2_credits_xattr(inode);
  1100. retry:
  1101. handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
  1102. if (IS_ERR(handle)) {
  1103. error = PTR_ERR(handle);
  1104. } else {
  1105. int error2;
  1106. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  1107. value, value_len, flags);
  1108. error2 = ext4_journal_stop(handle);
  1109. if (error == -ENOSPC &&
  1110. ext4_should_retry_alloc(inode->i_sb, &retries))
  1111. goto retry;
  1112. if (error == 0)
  1113. error = error2;
  1114. }
  1115. return error;
  1116. }
  1117. /*
  1118. * Shift the EA entries in the inode to create space for the increased
  1119. * i_extra_isize.
  1120. */
  1121. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  1122. int value_offs_shift, void *to,
  1123. void *from, size_t n, int blocksize)
  1124. {
  1125. struct ext4_xattr_entry *last = entry;
  1126. int new_offs;
  1127. /* Adjust the value offsets of the entries */
  1128. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1129. if (!last->e_value_block && last->e_value_size) {
  1130. new_offs = le16_to_cpu(last->e_value_offs) +
  1131. value_offs_shift;
  1132. BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
  1133. > blocksize);
  1134. last->e_value_offs = cpu_to_le16(new_offs);
  1135. }
  1136. }
  1137. /* Shift the entries by n bytes */
  1138. memmove(to, from, n);
  1139. }
  1140. /*
  1141. * Expand an inode by new_extra_isize bytes when EAs are present.
  1142. * Returns 0 on success or negative error number on failure.
  1143. */
  1144. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1145. struct ext4_inode *raw_inode, handle_t *handle)
  1146. {
  1147. struct ext4_xattr_ibody_header *header;
  1148. struct ext4_xattr_entry *entry, *last, *first;
  1149. struct buffer_head *bh = NULL;
  1150. struct ext4_xattr_ibody_find *is = NULL;
  1151. struct ext4_xattr_block_find *bs = NULL;
  1152. char *buffer = NULL, *b_entry_name = NULL;
  1153. size_t min_offs, free;
  1154. int total_ino;
  1155. void *base, *start, *end;
  1156. int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
  1157. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1158. down_write(&EXT4_I(inode)->xattr_sem);
  1159. retry:
  1160. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
  1161. up_write(&EXT4_I(inode)->xattr_sem);
  1162. return 0;
  1163. }
  1164. header = IHDR(inode, raw_inode);
  1165. entry = IFIRST(header);
  1166. /*
  1167. * Check if enough free space is available in the inode to shift the
  1168. * entries ahead by new_extra_isize.
  1169. */
  1170. base = start = entry;
  1171. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1172. min_offs = end - base;
  1173. last = entry;
  1174. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1175. free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
  1176. if (free >= new_extra_isize) {
  1177. entry = IFIRST(header);
  1178. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1179. - new_extra_isize, (void *)raw_inode +
  1180. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1181. (void *)header, total_ino,
  1182. inode->i_sb->s_blocksize);
  1183. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1184. error = 0;
  1185. goto cleanup;
  1186. }
  1187. /*
  1188. * Enough free space isn't available in the inode, check if
  1189. * EA block can hold new_extra_isize bytes.
  1190. */
  1191. if (EXT4_I(inode)->i_file_acl) {
  1192. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1193. error = -EIO;
  1194. if (!bh)
  1195. goto cleanup;
  1196. if (ext4_xattr_check_block(inode, bh)) {
  1197. EXT4_ERROR_INODE(inode, "bad block %llu",
  1198. EXT4_I(inode)->i_file_acl);
  1199. error = -EIO;
  1200. goto cleanup;
  1201. }
  1202. base = BHDR(bh);
  1203. first = BFIRST(bh);
  1204. end = bh->b_data + bh->b_size;
  1205. min_offs = end - base;
  1206. free = ext4_xattr_free_space(first, &min_offs, base, NULL);
  1207. if (free < new_extra_isize) {
  1208. if (!tried_min_extra_isize && s_min_extra_isize) {
  1209. tried_min_extra_isize++;
  1210. new_extra_isize = s_min_extra_isize;
  1211. brelse(bh);
  1212. goto retry;
  1213. }
  1214. error = -1;
  1215. goto cleanup;
  1216. }
  1217. } else {
  1218. free = inode->i_sb->s_blocksize;
  1219. }
  1220. while (new_extra_isize > 0) {
  1221. size_t offs, size, entry_size;
  1222. struct ext4_xattr_entry *small_entry = NULL;
  1223. struct ext4_xattr_info i = {
  1224. .value = NULL,
  1225. .value_len = 0,
  1226. };
  1227. unsigned int total_size; /* EA entry size + value size */
  1228. unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
  1229. unsigned int min_total_size = ~0U;
  1230. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1231. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1232. if (!is || !bs) {
  1233. error = -ENOMEM;
  1234. goto cleanup;
  1235. }
  1236. is->s.not_found = -ENODATA;
  1237. bs->s.not_found = -ENODATA;
  1238. is->iloc.bh = NULL;
  1239. bs->bh = NULL;
  1240. last = IFIRST(header);
  1241. /* Find the entry best suited to be pushed into EA block */
  1242. entry = NULL;
  1243. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1244. total_size =
  1245. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1246. EXT4_XATTR_LEN(last->e_name_len);
  1247. if (total_size <= free && total_size < min_total_size) {
  1248. if (total_size < new_extra_isize) {
  1249. small_entry = last;
  1250. } else {
  1251. entry = last;
  1252. min_total_size = total_size;
  1253. }
  1254. }
  1255. }
  1256. if (entry == NULL) {
  1257. if (small_entry) {
  1258. entry = small_entry;
  1259. } else {
  1260. if (!tried_min_extra_isize &&
  1261. s_min_extra_isize) {
  1262. tried_min_extra_isize++;
  1263. new_extra_isize = s_min_extra_isize;
  1264. kfree(is); is = NULL;
  1265. kfree(bs); bs = NULL;
  1266. brelse(bh);
  1267. goto retry;
  1268. }
  1269. error = -1;
  1270. goto cleanup;
  1271. }
  1272. }
  1273. offs = le16_to_cpu(entry->e_value_offs);
  1274. size = le32_to_cpu(entry->e_value_size);
  1275. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1276. i.name_index = entry->e_name_index,
  1277. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
  1278. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1279. if (!buffer || !b_entry_name) {
  1280. error = -ENOMEM;
  1281. goto cleanup;
  1282. }
  1283. /* Save the entry name and the entry value */
  1284. memcpy(buffer, (void *)IFIRST(header) + offs,
  1285. EXT4_XATTR_SIZE(size));
  1286. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1287. b_entry_name[entry->e_name_len] = '\0';
  1288. i.name = b_entry_name;
  1289. error = ext4_get_inode_loc(inode, &is->iloc);
  1290. if (error)
  1291. goto cleanup;
  1292. error = ext4_xattr_ibody_find(inode, &i, is);
  1293. if (error)
  1294. goto cleanup;
  1295. /* Remove the chosen entry from the inode */
  1296. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1297. if (error)
  1298. goto cleanup;
  1299. entry = IFIRST(header);
  1300. if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
  1301. shift_bytes = new_extra_isize;
  1302. else
  1303. shift_bytes = entry_size + size;
  1304. /* Adjust the offsets and shift the remaining entries ahead */
  1305. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
  1306. shift_bytes, (void *)raw_inode +
  1307. EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
  1308. (void *)header, total_ino - entry_size,
  1309. inode->i_sb->s_blocksize);
  1310. extra_isize += shift_bytes;
  1311. new_extra_isize -= shift_bytes;
  1312. EXT4_I(inode)->i_extra_isize = extra_isize;
  1313. i.name = b_entry_name;
  1314. i.value = buffer;
  1315. i.value_len = size;
  1316. error = ext4_xattr_block_find(inode, &i, bs);
  1317. if (error)
  1318. goto cleanup;
  1319. /* Add entry which was removed from the inode into the block */
  1320. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1321. if (error)
  1322. goto cleanup;
  1323. kfree(b_entry_name);
  1324. kfree(buffer);
  1325. b_entry_name = NULL;
  1326. buffer = NULL;
  1327. brelse(is->iloc.bh);
  1328. kfree(is);
  1329. kfree(bs);
  1330. }
  1331. brelse(bh);
  1332. up_write(&EXT4_I(inode)->xattr_sem);
  1333. return 0;
  1334. cleanup:
  1335. kfree(b_entry_name);
  1336. kfree(buffer);
  1337. if (is)
  1338. brelse(is->iloc.bh);
  1339. kfree(is);
  1340. kfree(bs);
  1341. brelse(bh);
  1342. up_write(&EXT4_I(inode)->xattr_sem);
  1343. return error;
  1344. }
  1345. /*
  1346. * ext4_xattr_delete_inode()
  1347. *
  1348. * Free extended attribute resources associated with this inode. This
  1349. * is called immediately before an inode is freed. We have exclusive
  1350. * access to the inode.
  1351. */
  1352. void
  1353. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1354. {
  1355. struct buffer_head *bh = NULL;
  1356. if (!EXT4_I(inode)->i_file_acl)
  1357. goto cleanup;
  1358. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1359. if (!bh) {
  1360. EXT4_ERROR_INODE(inode, "block %llu read error",
  1361. EXT4_I(inode)->i_file_acl);
  1362. goto cleanup;
  1363. }
  1364. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1365. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1366. EXT4_ERROR_INODE(inode, "bad block %llu",
  1367. EXT4_I(inode)->i_file_acl);
  1368. goto cleanup;
  1369. }
  1370. ext4_xattr_release_block(handle, inode, bh);
  1371. EXT4_I(inode)->i_file_acl = 0;
  1372. cleanup:
  1373. brelse(bh);
  1374. }
  1375. /*
  1376. * ext4_xattr_put_super()
  1377. *
  1378. * This is called when a file system is unmounted.
  1379. */
  1380. void
  1381. ext4_xattr_put_super(struct super_block *sb)
  1382. {
  1383. mb_cache_shrink(sb->s_bdev);
  1384. }
  1385. /*
  1386. * ext4_xattr_cache_insert()
  1387. *
  1388. * Create a new entry in the extended attribute cache, and insert
  1389. * it unless such an entry is already in the cache.
  1390. *
  1391. * Returns 0, or a negative error number on failure.
  1392. */
  1393. static void
  1394. ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
  1395. {
  1396. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1397. struct mb_cache_entry *ce;
  1398. int error;
  1399. ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
  1400. if (!ce) {
  1401. ea_bdebug(bh, "out of memory");
  1402. return;
  1403. }
  1404. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
  1405. if (error) {
  1406. mb_cache_entry_free(ce);
  1407. if (error == -EBUSY) {
  1408. ea_bdebug(bh, "already in cache");
  1409. error = 0;
  1410. }
  1411. } else {
  1412. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1413. mb_cache_entry_release(ce);
  1414. }
  1415. }
  1416. /*
  1417. * ext4_xattr_cmp()
  1418. *
  1419. * Compare two extended attribute blocks for equality.
  1420. *
  1421. * Returns 0 if the blocks are equal, 1 if they differ, and
  1422. * a negative error number on errors.
  1423. */
  1424. static int
  1425. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1426. struct ext4_xattr_header *header2)
  1427. {
  1428. struct ext4_xattr_entry *entry1, *entry2;
  1429. entry1 = ENTRY(header1+1);
  1430. entry2 = ENTRY(header2+1);
  1431. while (!IS_LAST_ENTRY(entry1)) {
  1432. if (IS_LAST_ENTRY(entry2))
  1433. return 1;
  1434. if (entry1->e_hash != entry2->e_hash ||
  1435. entry1->e_name_index != entry2->e_name_index ||
  1436. entry1->e_name_len != entry2->e_name_len ||
  1437. entry1->e_value_size != entry2->e_value_size ||
  1438. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1439. return 1;
  1440. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1441. return -EIO;
  1442. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1443. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1444. le32_to_cpu(entry1->e_value_size)))
  1445. return 1;
  1446. entry1 = EXT4_XATTR_NEXT(entry1);
  1447. entry2 = EXT4_XATTR_NEXT(entry2);
  1448. }
  1449. if (!IS_LAST_ENTRY(entry2))
  1450. return 1;
  1451. return 0;
  1452. }
  1453. /*
  1454. * ext4_xattr_cache_find()
  1455. *
  1456. * Find an identical extended attribute block.
  1457. *
  1458. * Returns a pointer to the block found, or NULL if such a block was
  1459. * not found or an error occurred.
  1460. */
  1461. static struct buffer_head *
  1462. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1463. struct mb_cache_entry **pce)
  1464. {
  1465. __u32 hash = le32_to_cpu(header->h_hash);
  1466. struct mb_cache_entry *ce;
  1467. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  1468. if (!header->h_hash)
  1469. return NULL; /* never share */
  1470. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1471. again:
  1472. ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
  1473. hash);
  1474. while (ce) {
  1475. struct buffer_head *bh;
  1476. if (IS_ERR(ce)) {
  1477. if (PTR_ERR(ce) == -EAGAIN)
  1478. goto again;
  1479. break;
  1480. }
  1481. bh = sb_bread(inode->i_sb, ce->e_block);
  1482. if (!bh) {
  1483. EXT4_ERROR_INODE(inode, "block %lu read error",
  1484. (unsigned long) ce->e_block);
  1485. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1486. EXT4_XATTR_REFCOUNT_MAX) {
  1487. ea_idebug(inode, "block %lu refcount %d>=%d",
  1488. (unsigned long) ce->e_block,
  1489. le32_to_cpu(BHDR(bh)->h_refcount),
  1490. EXT4_XATTR_REFCOUNT_MAX);
  1491. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1492. *pce = ce;
  1493. return bh;
  1494. }
  1495. brelse(bh);
  1496. ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
  1497. }
  1498. return NULL;
  1499. }
  1500. #define NAME_HASH_SHIFT 5
  1501. #define VALUE_HASH_SHIFT 16
  1502. /*
  1503. * ext4_xattr_hash_entry()
  1504. *
  1505. * Compute the hash of an extended attribute.
  1506. */
  1507. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1508. struct ext4_xattr_entry *entry)
  1509. {
  1510. __u32 hash = 0;
  1511. char *name = entry->e_name;
  1512. int n;
  1513. for (n = 0; n < entry->e_name_len; n++) {
  1514. hash = (hash << NAME_HASH_SHIFT) ^
  1515. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1516. *name++;
  1517. }
  1518. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1519. __le32 *value = (__le32 *)((char *)header +
  1520. le16_to_cpu(entry->e_value_offs));
  1521. for (n = (le32_to_cpu(entry->e_value_size) +
  1522. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1523. hash = (hash << VALUE_HASH_SHIFT) ^
  1524. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1525. le32_to_cpu(*value++);
  1526. }
  1527. }
  1528. entry->e_hash = cpu_to_le32(hash);
  1529. }
  1530. #undef NAME_HASH_SHIFT
  1531. #undef VALUE_HASH_SHIFT
  1532. #define BLOCK_HASH_SHIFT 16
  1533. /*
  1534. * ext4_xattr_rehash()
  1535. *
  1536. * Re-compute the extended attribute hash value after an entry has changed.
  1537. */
  1538. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1539. struct ext4_xattr_entry *entry)
  1540. {
  1541. struct ext4_xattr_entry *here;
  1542. __u32 hash = 0;
  1543. ext4_xattr_hash_entry(header, entry);
  1544. here = ENTRY(header+1);
  1545. while (!IS_LAST_ENTRY(here)) {
  1546. if (!here->e_hash) {
  1547. /* Block is not shared if an entry's hash value == 0 */
  1548. hash = 0;
  1549. break;
  1550. }
  1551. hash = (hash << BLOCK_HASH_SHIFT) ^
  1552. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1553. le32_to_cpu(here->e_hash);
  1554. here = EXT4_XATTR_NEXT(here);
  1555. }
  1556. header->h_hash = cpu_to_le32(hash);
  1557. }
  1558. #undef BLOCK_HASH_SHIFT
  1559. #define HASH_BUCKET_BITS 10
  1560. struct mb_cache *
  1561. ext4_xattr_create_cache(char *name)
  1562. {
  1563. return mb_cache_create(name, HASH_BUCKET_BITS);
  1564. }
  1565. void ext4_xattr_destroy_cache(struct mb_cache *cache)
  1566. {
  1567. if (cache)
  1568. mb_cache_destroy(cache);
  1569. }