xattr.c 42 KB

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