xattr.c 44 KB

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