xattr.c 44 KB

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