xattr.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  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_EXT4DEV_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_EXT4DEV_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_EXT4DEV_FS_POSIX_ACL
  108. &ext4_xattr_acl_access_handler,
  109. &ext4_xattr_acl_default_handler,
  110. #endif
  111. #ifdef CONFIG_EXT4DEV_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. struct super_block *sb = inode->i_sb;
  632. struct buffer_head *new_bh = NULL;
  633. struct ext4_xattr_search *s = &bs->s;
  634. struct mb_cache_entry *ce = NULL;
  635. int error = 0;
  636. #define header(x) ((struct ext4_xattr_header *)(x))
  637. if (i->value && i->value_len > sb->s_blocksize)
  638. return -ENOSPC;
  639. if (s->base) {
  640. ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
  641. bs->bh->b_blocknr);
  642. error = ext4_journal_get_write_access(handle, bs->bh);
  643. if (error)
  644. goto cleanup;
  645. lock_buffer(bs->bh);
  646. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  647. if (ce) {
  648. mb_cache_entry_free(ce);
  649. ce = NULL;
  650. }
  651. ea_bdebug(bs->bh, "modifying in-place");
  652. error = ext4_xattr_set_entry(i, s);
  653. if (!error) {
  654. if (!IS_LAST_ENTRY(s->first))
  655. ext4_xattr_rehash(header(s->base),
  656. s->here);
  657. ext4_xattr_cache_insert(bs->bh);
  658. }
  659. unlock_buffer(bs->bh);
  660. if (error == -EIO)
  661. goto bad_block;
  662. if (!error)
  663. error = ext4_journal_dirty_metadata(handle,
  664. bs->bh);
  665. if (error)
  666. goto cleanup;
  667. goto inserted;
  668. } else {
  669. int offset = (char *)s->here - bs->bh->b_data;
  670. unlock_buffer(bs->bh);
  671. jbd2_journal_release_buffer(handle, bs->bh);
  672. if (ce) {
  673. mb_cache_entry_release(ce);
  674. ce = NULL;
  675. }
  676. ea_bdebug(bs->bh, "cloning");
  677. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  678. error = -ENOMEM;
  679. if (s->base == NULL)
  680. goto cleanup;
  681. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  682. s->first = ENTRY(header(s->base)+1);
  683. header(s->base)->h_refcount = cpu_to_le32(1);
  684. s->here = ENTRY(s->base + offset);
  685. s->end = s->base + bs->bh->b_size;
  686. }
  687. } else {
  688. /* Allocate a buffer where we construct the new block. */
  689. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  690. /* assert(header == s->base) */
  691. error = -ENOMEM;
  692. if (s->base == NULL)
  693. goto cleanup;
  694. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  695. header(s->base)->h_blocks = cpu_to_le32(1);
  696. header(s->base)->h_refcount = cpu_to_le32(1);
  697. s->first = ENTRY(header(s->base)+1);
  698. s->here = ENTRY(header(s->base)+1);
  699. s->end = s->base + sb->s_blocksize;
  700. }
  701. error = ext4_xattr_set_entry(i, s);
  702. if (error == -EIO)
  703. goto bad_block;
  704. if (error)
  705. goto cleanup;
  706. if (!IS_LAST_ENTRY(s->first))
  707. ext4_xattr_rehash(header(s->base), s->here);
  708. inserted:
  709. if (!IS_LAST_ENTRY(s->first)) {
  710. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  711. if (new_bh) {
  712. /* We found an identical block in the cache. */
  713. if (new_bh == bs->bh)
  714. ea_bdebug(new_bh, "keeping");
  715. else {
  716. /* The old block is released after updating
  717. the inode. */
  718. error = -EDQUOT;
  719. if (DQUOT_ALLOC_BLOCK(inode, 1))
  720. goto cleanup;
  721. error = ext4_journal_get_write_access(handle,
  722. new_bh);
  723. if (error)
  724. goto cleanup_dquot;
  725. lock_buffer(new_bh);
  726. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  727. ea_bdebug(new_bh, "reusing; refcount now=%d",
  728. le32_to_cpu(BHDR(new_bh)->h_refcount));
  729. unlock_buffer(new_bh);
  730. error = ext4_journal_dirty_metadata(handle,
  731. new_bh);
  732. if (error)
  733. goto cleanup_dquot;
  734. }
  735. mb_cache_entry_release(ce);
  736. ce = NULL;
  737. } else if (bs->bh && s->base == bs->bh->b_data) {
  738. /* We were modifying this block in-place. */
  739. ea_bdebug(bs->bh, "keeping this block");
  740. new_bh = bs->bh;
  741. get_bh(new_bh);
  742. } else {
  743. /* We need to allocate a new block */
  744. ext4_fsblk_t goal = ext4_group_first_block_no(sb,
  745. EXT4_I(inode)->i_block_group);
  746. ext4_fsblk_t block = ext4_new_block(handle, inode,
  747. goal, &error);
  748. if (error)
  749. goto cleanup;
  750. ea_idebug(inode, "creating block %d", block);
  751. new_bh = sb_getblk(sb, block);
  752. if (!new_bh) {
  753. getblk_failed:
  754. ext4_free_blocks(handle, inode, block, 1, 1);
  755. error = -EIO;
  756. goto cleanup;
  757. }
  758. lock_buffer(new_bh);
  759. error = ext4_journal_get_create_access(handle, new_bh);
  760. if (error) {
  761. unlock_buffer(new_bh);
  762. goto getblk_failed;
  763. }
  764. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  765. set_buffer_uptodate(new_bh);
  766. unlock_buffer(new_bh);
  767. ext4_xattr_cache_insert(new_bh);
  768. error = ext4_journal_dirty_metadata(handle, new_bh);
  769. if (error)
  770. goto cleanup;
  771. }
  772. }
  773. /* Update the inode. */
  774. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  775. /* Drop the previous xattr block. */
  776. if (bs->bh && bs->bh != new_bh)
  777. ext4_xattr_release_block(handle, inode, bs->bh);
  778. error = 0;
  779. cleanup:
  780. if (ce)
  781. mb_cache_entry_release(ce);
  782. brelse(new_bh);
  783. if (!(bs->bh && s->base == bs->bh->b_data))
  784. kfree(s->base);
  785. return error;
  786. cleanup_dquot:
  787. DQUOT_FREE_BLOCK(inode, 1);
  788. goto cleanup;
  789. bad_block:
  790. ext4_error(inode->i_sb, __func__,
  791. "inode %lu: bad block %llu", inode->i_ino,
  792. EXT4_I(inode)->i_file_acl);
  793. goto cleanup;
  794. #undef header
  795. }
  796. struct ext4_xattr_ibody_find {
  797. struct ext4_xattr_search s;
  798. struct ext4_iloc iloc;
  799. };
  800. static int
  801. ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  802. struct ext4_xattr_ibody_find *is)
  803. {
  804. struct ext4_xattr_ibody_header *header;
  805. struct ext4_inode *raw_inode;
  806. int error;
  807. if (EXT4_I(inode)->i_extra_isize == 0)
  808. return 0;
  809. raw_inode = ext4_raw_inode(&is->iloc);
  810. header = IHDR(inode, raw_inode);
  811. is->s.base = is->s.first = IFIRST(header);
  812. is->s.here = is->s.first;
  813. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  814. if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
  815. error = ext4_xattr_check_names(IFIRST(header), is->s.end);
  816. if (error)
  817. return error;
  818. /* Find the named attribute. */
  819. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  820. i->name, is->s.end -
  821. (void *)is->s.base, 0);
  822. if (error && error != -ENODATA)
  823. return error;
  824. is->s.not_found = error;
  825. }
  826. return 0;
  827. }
  828. static int
  829. ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  830. struct ext4_xattr_info *i,
  831. struct ext4_xattr_ibody_find *is)
  832. {
  833. struct ext4_xattr_ibody_header *header;
  834. struct ext4_xattr_search *s = &is->s;
  835. int error;
  836. if (EXT4_I(inode)->i_extra_isize == 0)
  837. return -ENOSPC;
  838. error = ext4_xattr_set_entry(i, s);
  839. if (error)
  840. return error;
  841. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  842. if (!IS_LAST_ENTRY(s->first)) {
  843. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  844. EXT4_I(inode)->i_state |= EXT4_STATE_XATTR;
  845. } else {
  846. header->h_magic = cpu_to_le32(0);
  847. EXT4_I(inode)->i_state &= ~EXT4_STATE_XATTR;
  848. }
  849. return 0;
  850. }
  851. /*
  852. * ext4_xattr_set_handle()
  853. *
  854. * Create, replace or remove an extended attribute for this inode. Buffer
  855. * is NULL to remove an existing extended attribute, and non-NULL to
  856. * either replace an existing extended attribute, or create a new extended
  857. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  858. * specify that an extended attribute must exist and must not exist
  859. * previous to the call, respectively.
  860. *
  861. * Returns 0, or a negative error number on failure.
  862. */
  863. int
  864. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  865. const char *name, const void *value, size_t value_len,
  866. int flags)
  867. {
  868. struct ext4_xattr_info i = {
  869. .name_index = name_index,
  870. .name = name,
  871. .value = value,
  872. .value_len = value_len,
  873. };
  874. struct ext4_xattr_ibody_find is = {
  875. .s = { .not_found = -ENODATA, },
  876. };
  877. struct ext4_xattr_block_find bs = {
  878. .s = { .not_found = -ENODATA, },
  879. };
  880. int error;
  881. if (!name)
  882. return -EINVAL;
  883. if (strlen(name) > 255)
  884. return -ERANGE;
  885. down_write(&EXT4_I(inode)->xattr_sem);
  886. error = ext4_get_inode_loc(inode, &is.iloc);
  887. if (error)
  888. goto cleanup;
  889. if (EXT4_I(inode)->i_state & EXT4_STATE_NEW) {
  890. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  891. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  892. EXT4_I(inode)->i_state &= ~EXT4_STATE_NEW;
  893. }
  894. error = ext4_xattr_ibody_find(inode, &i, &is);
  895. if (error)
  896. goto cleanup;
  897. if (is.s.not_found)
  898. error = ext4_xattr_block_find(inode, &i, &bs);
  899. if (error)
  900. goto cleanup;
  901. if (is.s.not_found && bs.s.not_found) {
  902. error = -ENODATA;
  903. if (flags & XATTR_REPLACE)
  904. goto cleanup;
  905. error = 0;
  906. if (!value)
  907. goto cleanup;
  908. } else {
  909. error = -EEXIST;
  910. if (flags & XATTR_CREATE)
  911. goto cleanup;
  912. }
  913. error = ext4_journal_get_write_access(handle, is.iloc.bh);
  914. if (error)
  915. goto cleanup;
  916. if (!value) {
  917. if (!is.s.not_found)
  918. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  919. else if (!bs.s.not_found)
  920. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  921. } else {
  922. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  923. if (!error && !bs.s.not_found) {
  924. i.value = NULL;
  925. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  926. } else if (error == -ENOSPC) {
  927. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  928. if (error)
  929. goto cleanup;
  930. if (!is.s.not_found) {
  931. i.value = NULL;
  932. error = ext4_xattr_ibody_set(handle, inode, &i,
  933. &is);
  934. }
  935. }
  936. }
  937. if (!error) {
  938. ext4_xattr_update_super_block(handle, inode->i_sb);
  939. inode->i_ctime = ext4_current_time(inode);
  940. if (!value)
  941. EXT4_I(inode)->i_state &= ~EXT4_STATE_NO_EXPAND;
  942. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  943. /*
  944. * The bh is consumed by ext4_mark_iloc_dirty, even with
  945. * error != 0.
  946. */
  947. is.iloc.bh = NULL;
  948. if (IS_SYNC(inode))
  949. handle->h_sync = 1;
  950. }
  951. cleanup:
  952. brelse(is.iloc.bh);
  953. brelse(bs.bh);
  954. up_write(&EXT4_I(inode)->xattr_sem);
  955. return error;
  956. }
  957. /*
  958. * ext4_xattr_set()
  959. *
  960. * Like ext4_xattr_set_handle, but start from an inode. This extended
  961. * attribute modification is a filesystem transaction by itself.
  962. *
  963. * Returns 0, or a negative error number on failure.
  964. */
  965. int
  966. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  967. const void *value, size_t value_len, int flags)
  968. {
  969. handle_t *handle;
  970. int error, retries = 0;
  971. retry:
  972. handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  973. if (IS_ERR(handle)) {
  974. error = PTR_ERR(handle);
  975. } else {
  976. int error2;
  977. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  978. value, value_len, flags);
  979. error2 = ext4_journal_stop(handle);
  980. if (error == -ENOSPC &&
  981. ext4_should_retry_alloc(inode->i_sb, &retries))
  982. goto retry;
  983. if (error == 0)
  984. error = error2;
  985. }
  986. return error;
  987. }
  988. /*
  989. * Shift the EA entries in the inode to create space for the increased
  990. * i_extra_isize.
  991. */
  992. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  993. int value_offs_shift, void *to,
  994. void *from, size_t n, int blocksize)
  995. {
  996. struct ext4_xattr_entry *last = entry;
  997. int new_offs;
  998. /* Adjust the value offsets of the entries */
  999. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1000. if (!last->e_value_block && last->e_value_size) {
  1001. new_offs = le16_to_cpu(last->e_value_offs) +
  1002. value_offs_shift;
  1003. BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
  1004. > blocksize);
  1005. last->e_value_offs = cpu_to_le16(new_offs);
  1006. }
  1007. }
  1008. /* Shift the entries by n bytes */
  1009. memmove(to, from, n);
  1010. }
  1011. /*
  1012. * Expand an inode by new_extra_isize bytes when EAs are present.
  1013. * Returns 0 on success or negative error number on failure.
  1014. */
  1015. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1016. struct ext4_inode *raw_inode, handle_t *handle)
  1017. {
  1018. struct ext4_xattr_ibody_header *header;
  1019. struct ext4_xattr_entry *entry, *last, *first;
  1020. struct buffer_head *bh = NULL;
  1021. struct ext4_xattr_ibody_find *is = NULL;
  1022. struct ext4_xattr_block_find *bs = NULL;
  1023. char *buffer = NULL, *b_entry_name = NULL;
  1024. size_t min_offs, free;
  1025. int total_ino, total_blk;
  1026. void *base, *start, *end;
  1027. int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
  1028. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1029. down_write(&EXT4_I(inode)->xattr_sem);
  1030. retry:
  1031. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
  1032. up_write(&EXT4_I(inode)->xattr_sem);
  1033. return 0;
  1034. }
  1035. header = IHDR(inode, raw_inode);
  1036. entry = IFIRST(header);
  1037. /*
  1038. * Check if enough free space is available in the inode to shift the
  1039. * entries ahead by new_extra_isize.
  1040. */
  1041. base = start = entry;
  1042. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1043. min_offs = end - base;
  1044. last = entry;
  1045. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1046. free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
  1047. if (free >= new_extra_isize) {
  1048. entry = IFIRST(header);
  1049. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1050. - new_extra_isize, (void *)raw_inode +
  1051. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1052. (void *)header, total_ino,
  1053. inode->i_sb->s_blocksize);
  1054. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1055. error = 0;
  1056. goto cleanup;
  1057. }
  1058. /*
  1059. * Enough free space isn't available in the inode, check if
  1060. * EA block can hold new_extra_isize bytes.
  1061. */
  1062. if (EXT4_I(inode)->i_file_acl) {
  1063. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1064. error = -EIO;
  1065. if (!bh)
  1066. goto cleanup;
  1067. if (ext4_xattr_check_block(bh)) {
  1068. ext4_error(inode->i_sb, __func__,
  1069. "inode %lu: bad block %llu", inode->i_ino,
  1070. EXT4_I(inode)->i_file_acl);
  1071. error = -EIO;
  1072. goto cleanup;
  1073. }
  1074. base = BHDR(bh);
  1075. first = BFIRST(bh);
  1076. end = bh->b_data + bh->b_size;
  1077. min_offs = end - base;
  1078. free = ext4_xattr_free_space(first, &min_offs, base,
  1079. &total_blk);
  1080. if (free < new_extra_isize) {
  1081. if (!tried_min_extra_isize && s_min_extra_isize) {
  1082. tried_min_extra_isize++;
  1083. new_extra_isize = s_min_extra_isize;
  1084. brelse(bh);
  1085. goto retry;
  1086. }
  1087. error = -1;
  1088. goto cleanup;
  1089. }
  1090. } else {
  1091. free = inode->i_sb->s_blocksize;
  1092. }
  1093. while (new_extra_isize > 0) {
  1094. size_t offs, size, entry_size;
  1095. struct ext4_xattr_entry *small_entry = NULL;
  1096. struct ext4_xattr_info i = {
  1097. .value = NULL,
  1098. .value_len = 0,
  1099. };
  1100. unsigned int total_size; /* EA entry size + value size */
  1101. unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
  1102. unsigned int min_total_size = ~0U;
  1103. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1104. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1105. if (!is || !bs) {
  1106. error = -ENOMEM;
  1107. goto cleanup;
  1108. }
  1109. is->s.not_found = -ENODATA;
  1110. bs->s.not_found = -ENODATA;
  1111. is->iloc.bh = NULL;
  1112. bs->bh = NULL;
  1113. last = IFIRST(header);
  1114. /* Find the entry best suited to be pushed into EA block */
  1115. entry = NULL;
  1116. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1117. total_size =
  1118. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1119. EXT4_XATTR_LEN(last->e_name_len);
  1120. if (total_size <= free && total_size < min_total_size) {
  1121. if (total_size < new_extra_isize) {
  1122. small_entry = last;
  1123. } else {
  1124. entry = last;
  1125. min_total_size = total_size;
  1126. }
  1127. }
  1128. }
  1129. if (entry == NULL) {
  1130. if (small_entry) {
  1131. entry = small_entry;
  1132. } else {
  1133. if (!tried_min_extra_isize &&
  1134. s_min_extra_isize) {
  1135. tried_min_extra_isize++;
  1136. new_extra_isize = s_min_extra_isize;
  1137. goto retry;
  1138. }
  1139. error = -1;
  1140. goto cleanup;
  1141. }
  1142. }
  1143. offs = le16_to_cpu(entry->e_value_offs);
  1144. size = le32_to_cpu(entry->e_value_size);
  1145. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1146. i.name_index = entry->e_name_index,
  1147. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
  1148. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1149. if (!buffer || !b_entry_name) {
  1150. error = -ENOMEM;
  1151. goto cleanup;
  1152. }
  1153. /* Save the entry name and the entry value */
  1154. memcpy(buffer, (void *)IFIRST(header) + offs,
  1155. EXT4_XATTR_SIZE(size));
  1156. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1157. b_entry_name[entry->e_name_len] = '\0';
  1158. i.name = b_entry_name;
  1159. error = ext4_get_inode_loc(inode, &is->iloc);
  1160. if (error)
  1161. goto cleanup;
  1162. error = ext4_xattr_ibody_find(inode, &i, is);
  1163. if (error)
  1164. goto cleanup;
  1165. /* Remove the chosen entry from the inode */
  1166. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1167. entry = IFIRST(header);
  1168. if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
  1169. shift_bytes = new_extra_isize;
  1170. else
  1171. shift_bytes = entry_size + size;
  1172. /* Adjust the offsets and shift the remaining entries ahead */
  1173. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
  1174. shift_bytes, (void *)raw_inode +
  1175. EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
  1176. (void *)header, total_ino - entry_size,
  1177. inode->i_sb->s_blocksize);
  1178. extra_isize += shift_bytes;
  1179. new_extra_isize -= shift_bytes;
  1180. EXT4_I(inode)->i_extra_isize = extra_isize;
  1181. i.name = b_entry_name;
  1182. i.value = buffer;
  1183. i.value_len = size;
  1184. error = ext4_xattr_block_find(inode, &i, bs);
  1185. if (error)
  1186. goto cleanup;
  1187. /* Add entry which was removed from the inode into the block */
  1188. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1189. if (error)
  1190. goto cleanup;
  1191. kfree(b_entry_name);
  1192. kfree(buffer);
  1193. brelse(is->iloc.bh);
  1194. kfree(is);
  1195. kfree(bs);
  1196. }
  1197. brelse(bh);
  1198. up_write(&EXT4_I(inode)->xattr_sem);
  1199. return 0;
  1200. cleanup:
  1201. kfree(b_entry_name);
  1202. kfree(buffer);
  1203. if (is)
  1204. brelse(is->iloc.bh);
  1205. kfree(is);
  1206. kfree(bs);
  1207. brelse(bh);
  1208. up_write(&EXT4_I(inode)->xattr_sem);
  1209. return error;
  1210. }
  1211. /*
  1212. * ext4_xattr_delete_inode()
  1213. *
  1214. * Free extended attribute resources associated with this inode. This
  1215. * is called immediately before an inode is freed. We have exclusive
  1216. * access to the inode.
  1217. */
  1218. void
  1219. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1220. {
  1221. struct buffer_head *bh = NULL;
  1222. if (!EXT4_I(inode)->i_file_acl)
  1223. goto cleanup;
  1224. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1225. if (!bh) {
  1226. ext4_error(inode->i_sb, __func__,
  1227. "inode %lu: block %llu read error", inode->i_ino,
  1228. EXT4_I(inode)->i_file_acl);
  1229. goto cleanup;
  1230. }
  1231. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1232. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1233. ext4_error(inode->i_sb, __func__,
  1234. "inode %lu: bad block %llu", inode->i_ino,
  1235. EXT4_I(inode)->i_file_acl);
  1236. goto cleanup;
  1237. }
  1238. ext4_xattr_release_block(handle, inode, bh);
  1239. EXT4_I(inode)->i_file_acl = 0;
  1240. cleanup:
  1241. brelse(bh);
  1242. }
  1243. /*
  1244. * ext4_xattr_put_super()
  1245. *
  1246. * This is called when a file system is unmounted.
  1247. */
  1248. void
  1249. ext4_xattr_put_super(struct super_block *sb)
  1250. {
  1251. mb_cache_shrink(sb->s_bdev);
  1252. }
  1253. /*
  1254. * ext4_xattr_cache_insert()
  1255. *
  1256. * Create a new entry in the extended attribute cache, and insert
  1257. * it unless such an entry is already in the cache.
  1258. *
  1259. * Returns 0, or a negative error number on failure.
  1260. */
  1261. static void
  1262. ext4_xattr_cache_insert(struct buffer_head *bh)
  1263. {
  1264. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1265. struct mb_cache_entry *ce;
  1266. int error;
  1267. ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
  1268. if (!ce) {
  1269. ea_bdebug(bh, "out of memory");
  1270. return;
  1271. }
  1272. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
  1273. if (error) {
  1274. mb_cache_entry_free(ce);
  1275. if (error == -EBUSY) {
  1276. ea_bdebug(bh, "already in cache");
  1277. error = 0;
  1278. }
  1279. } else {
  1280. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1281. mb_cache_entry_release(ce);
  1282. }
  1283. }
  1284. /*
  1285. * ext4_xattr_cmp()
  1286. *
  1287. * Compare two extended attribute blocks for equality.
  1288. *
  1289. * Returns 0 if the blocks are equal, 1 if they differ, and
  1290. * a negative error number on errors.
  1291. */
  1292. static int
  1293. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1294. struct ext4_xattr_header *header2)
  1295. {
  1296. struct ext4_xattr_entry *entry1, *entry2;
  1297. entry1 = ENTRY(header1+1);
  1298. entry2 = ENTRY(header2+1);
  1299. while (!IS_LAST_ENTRY(entry1)) {
  1300. if (IS_LAST_ENTRY(entry2))
  1301. return 1;
  1302. if (entry1->e_hash != entry2->e_hash ||
  1303. entry1->e_name_index != entry2->e_name_index ||
  1304. entry1->e_name_len != entry2->e_name_len ||
  1305. entry1->e_value_size != entry2->e_value_size ||
  1306. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1307. return 1;
  1308. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1309. return -EIO;
  1310. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1311. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1312. le32_to_cpu(entry1->e_value_size)))
  1313. return 1;
  1314. entry1 = EXT4_XATTR_NEXT(entry1);
  1315. entry2 = EXT4_XATTR_NEXT(entry2);
  1316. }
  1317. if (!IS_LAST_ENTRY(entry2))
  1318. return 1;
  1319. return 0;
  1320. }
  1321. /*
  1322. * ext4_xattr_cache_find()
  1323. *
  1324. * Find an identical extended attribute block.
  1325. *
  1326. * Returns a pointer to the block found, or NULL if such a block was
  1327. * not found or an error occurred.
  1328. */
  1329. static struct buffer_head *
  1330. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1331. struct mb_cache_entry **pce)
  1332. {
  1333. __u32 hash = le32_to_cpu(header->h_hash);
  1334. struct mb_cache_entry *ce;
  1335. if (!header->h_hash)
  1336. return NULL; /* never share */
  1337. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1338. again:
  1339. ce = mb_cache_entry_find_first(ext4_xattr_cache, 0,
  1340. inode->i_sb->s_bdev, hash);
  1341. while (ce) {
  1342. struct buffer_head *bh;
  1343. if (IS_ERR(ce)) {
  1344. if (PTR_ERR(ce) == -EAGAIN)
  1345. goto again;
  1346. break;
  1347. }
  1348. bh = sb_bread(inode->i_sb, ce->e_block);
  1349. if (!bh) {
  1350. ext4_error(inode->i_sb, __func__,
  1351. "inode %lu: block %lu read error",
  1352. inode->i_ino, (unsigned long) ce->e_block);
  1353. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1354. EXT4_XATTR_REFCOUNT_MAX) {
  1355. ea_idebug(inode, "block %lu refcount %d>=%d",
  1356. (unsigned long) ce->e_block,
  1357. le32_to_cpu(BHDR(bh)->h_refcount),
  1358. EXT4_XATTR_REFCOUNT_MAX);
  1359. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1360. *pce = ce;
  1361. return bh;
  1362. }
  1363. brelse(bh);
  1364. ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
  1365. }
  1366. return NULL;
  1367. }
  1368. #define NAME_HASH_SHIFT 5
  1369. #define VALUE_HASH_SHIFT 16
  1370. /*
  1371. * ext4_xattr_hash_entry()
  1372. *
  1373. * Compute the hash of an extended attribute.
  1374. */
  1375. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1376. struct ext4_xattr_entry *entry)
  1377. {
  1378. __u32 hash = 0;
  1379. char *name = entry->e_name;
  1380. int n;
  1381. for (n=0; n < entry->e_name_len; n++) {
  1382. hash = (hash << NAME_HASH_SHIFT) ^
  1383. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1384. *name++;
  1385. }
  1386. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1387. __le32 *value = (__le32 *)((char *)header +
  1388. le16_to_cpu(entry->e_value_offs));
  1389. for (n = (le32_to_cpu(entry->e_value_size) +
  1390. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1391. hash = (hash << VALUE_HASH_SHIFT) ^
  1392. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1393. le32_to_cpu(*value++);
  1394. }
  1395. }
  1396. entry->e_hash = cpu_to_le32(hash);
  1397. }
  1398. #undef NAME_HASH_SHIFT
  1399. #undef VALUE_HASH_SHIFT
  1400. #define BLOCK_HASH_SHIFT 16
  1401. /*
  1402. * ext4_xattr_rehash()
  1403. *
  1404. * Re-compute the extended attribute hash value after an entry has changed.
  1405. */
  1406. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1407. struct ext4_xattr_entry *entry)
  1408. {
  1409. struct ext4_xattr_entry *here;
  1410. __u32 hash = 0;
  1411. ext4_xattr_hash_entry(header, entry);
  1412. here = ENTRY(header+1);
  1413. while (!IS_LAST_ENTRY(here)) {
  1414. if (!here->e_hash) {
  1415. /* Block is not shared if an entry's hash value == 0 */
  1416. hash = 0;
  1417. break;
  1418. }
  1419. hash = (hash << BLOCK_HASH_SHIFT) ^
  1420. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1421. le32_to_cpu(here->e_hash);
  1422. here = EXT4_XATTR_NEXT(here);
  1423. }
  1424. header->h_hash = cpu_to_le32(hash);
  1425. }
  1426. #undef BLOCK_HASH_SHIFT
  1427. int __init
  1428. init_ext4_xattr(void)
  1429. {
  1430. ext4_xattr_cache = mb_cache_create("ext4_xattr", NULL,
  1431. sizeof(struct mb_cache_entry) +
  1432. sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
  1433. if (!ext4_xattr_cache)
  1434. return -ENOMEM;
  1435. return 0;
  1436. }
  1437. void
  1438. exit_ext4_xattr(void)
  1439. {
  1440. if (ext4_xattr_cache)
  1441. mb_cache_destroy(ext4_xattr_cache);
  1442. ext4_xattr_cache = NULL;
  1443. }