xattr.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  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 dentry *dentry, char *buffer,
  91. size_t buffer_size);
  92. static struct mb_cache *ext4_xattr_cache;
  93. static const struct xattr_handler *ext4_xattr_handler_map[] = {
  94. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  95. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  96. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
  97. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
  98. #endif
  99. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  100. #ifdef CONFIG_EXT4_FS_SECURITY
  101. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  102. #endif
  103. };
  104. const struct xattr_handler *ext4_xattr_handlers[] = {
  105. &ext4_xattr_user_handler,
  106. &ext4_xattr_trusted_handler,
  107. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  108. &ext4_xattr_acl_access_handler,
  109. &ext4_xattr_acl_default_handler,
  110. #endif
  111. #ifdef CONFIG_EXT4_FS_SECURITY
  112. &ext4_xattr_security_handler,
  113. #endif
  114. NULL
  115. };
  116. static inline const struct xattr_handler *
  117. ext4_xattr_handler(int name_index)
  118. {
  119. const 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, 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. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  149. BHDR(bh)->h_blocks != cpu_to_le32(1))
  150. return -EIO;
  151. return ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  152. }
  153. static inline int
  154. ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
  155. {
  156. size_t value_size = le32_to_cpu(entry->e_value_size);
  157. if (entry->e_value_block != 0 || value_size > size ||
  158. le16_to_cpu(entry->e_value_offs) + value_size > size)
  159. return -EIO;
  160. return 0;
  161. }
  162. static int
  163. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  164. const char *name, size_t size, int sorted)
  165. {
  166. struct ext4_xattr_entry *entry;
  167. size_t name_len;
  168. int cmp = 1;
  169. if (name == NULL)
  170. return -EINVAL;
  171. name_len = strlen(name);
  172. entry = *pentry;
  173. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  174. cmp = name_index - entry->e_name_index;
  175. if (!cmp)
  176. cmp = name_len - entry->e_name_len;
  177. if (!cmp)
  178. cmp = memcmp(name, entry->e_name, name_len);
  179. if (cmp <= 0 && (sorted || cmp == 0))
  180. break;
  181. }
  182. *pentry = entry;
  183. if (!cmp && ext4_xattr_check_entry(entry, size))
  184. return -EIO;
  185. return cmp ? -ENODATA : 0;
  186. }
  187. static int
  188. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  189. void *buffer, size_t buffer_size)
  190. {
  191. struct buffer_head *bh = NULL;
  192. struct ext4_xattr_entry *entry;
  193. size_t size;
  194. int error;
  195. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  196. name_index, name, buffer, (long)buffer_size);
  197. error = -ENODATA;
  198. if (!EXT4_I(inode)->i_file_acl)
  199. goto cleanup;
  200. ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
  201. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  202. if (!bh)
  203. goto cleanup;
  204. ea_bdebug(bh, "b_count=%d, refcount=%d",
  205. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  206. if (ext4_xattr_check_block(bh)) {
  207. bad_block:
  208. EXT4_ERROR_INODE(inode, "bad block %llu",
  209. EXT4_I(inode)->i_file_acl);
  210. error = -EIO;
  211. goto cleanup;
  212. }
  213. ext4_xattr_cache_insert(bh);
  214. entry = BFIRST(bh);
  215. error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  216. if (error == -EIO)
  217. goto bad_block;
  218. if (error)
  219. goto cleanup;
  220. size = le32_to_cpu(entry->e_value_size);
  221. if (buffer) {
  222. error = -ERANGE;
  223. if (size > buffer_size)
  224. goto cleanup;
  225. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  226. size);
  227. }
  228. error = size;
  229. cleanup:
  230. brelse(bh);
  231. return error;
  232. }
  233. static int
  234. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  235. void *buffer, size_t buffer_size)
  236. {
  237. struct ext4_xattr_ibody_header *header;
  238. struct ext4_xattr_entry *entry;
  239. struct ext4_inode *raw_inode;
  240. struct ext4_iloc iloc;
  241. size_t size;
  242. void *end;
  243. int error;
  244. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  245. return -ENODATA;
  246. error = ext4_get_inode_loc(inode, &iloc);
  247. if (error)
  248. return error;
  249. raw_inode = ext4_raw_inode(&iloc);
  250. header = IHDR(inode, raw_inode);
  251. entry = IFIRST(header);
  252. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  253. error = ext4_xattr_check_names(entry, end);
  254. if (error)
  255. goto cleanup;
  256. error = ext4_xattr_find_entry(&entry, name_index, name,
  257. end - (void *)entry, 0);
  258. if (error)
  259. goto cleanup;
  260. size = le32_to_cpu(entry->e_value_size);
  261. if (buffer) {
  262. error = -ERANGE;
  263. if (size > buffer_size)
  264. goto cleanup;
  265. memcpy(buffer, (void *)IFIRST(header) +
  266. le16_to_cpu(entry->e_value_offs), size);
  267. }
  268. error = size;
  269. cleanup:
  270. brelse(iloc.bh);
  271. return error;
  272. }
  273. /*
  274. * ext4_xattr_get()
  275. *
  276. * Copy an extended attribute into the buffer
  277. * provided, or compute the buffer size required.
  278. * Buffer is NULL to compute the size of the buffer required.
  279. *
  280. * Returns a negative error number on failure, or the number of bytes
  281. * used / required on success.
  282. */
  283. int
  284. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  285. void *buffer, size_t buffer_size)
  286. {
  287. int error;
  288. down_read(&EXT4_I(inode)->xattr_sem);
  289. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  290. buffer_size);
  291. if (error == -ENODATA)
  292. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  293. buffer_size);
  294. up_read(&EXT4_I(inode)->xattr_sem);
  295. return error;
  296. }
  297. static int
  298. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  299. char *buffer, size_t buffer_size)
  300. {
  301. size_t rest = buffer_size;
  302. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  303. const struct xattr_handler *handler =
  304. ext4_xattr_handler(entry->e_name_index);
  305. if (handler) {
  306. size_t size = handler->list(dentry, buffer, rest,
  307. entry->e_name,
  308. entry->e_name_len,
  309. handler->flags);
  310. if (buffer) {
  311. if (size > rest)
  312. return -ERANGE;
  313. buffer += size;
  314. }
  315. rest -= size;
  316. }
  317. }
  318. return buffer_size - rest;
  319. }
  320. static int
  321. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  322. {
  323. struct inode *inode = dentry->d_inode;
  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(inode, "bad block %llu",
  340. EXT4_I(inode)->i_file_acl);
  341. error = -EIO;
  342. goto cleanup;
  343. }
  344. ext4_xattr_cache_insert(bh);
  345. error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  346. cleanup:
  347. brelse(bh);
  348. return error;
  349. }
  350. static int
  351. ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  352. {
  353. struct inode *inode = dentry->d_inode;
  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_test_inode_state(inode, 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(dentry, 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 dentry *dentry, char *buffer, size_t buffer_size)
  388. {
  389. int ret, ret2;
  390. down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
  391. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  392. if (ret < 0)
  393. goto errout;
  394. if (buffer) {
  395. buffer += ret;
  396. buffer_size -= ret;
  397. }
  398. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  399. if (ret < 0)
  400. goto errout;
  401. ret += ret2;
  402. errout:
  403. up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
  404. return ret;
  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. ext4_handle_dirty_super(handle, sb);
  418. }
  419. }
  420. /*
  421. * Release the xattr block BH: If the reference count is > 1, decrement
  422. * it; otherwise free the block.
  423. */
  424. static void
  425. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  426. struct buffer_head *bh)
  427. {
  428. struct mb_cache_entry *ce = NULL;
  429. int error = 0;
  430. ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
  431. error = ext4_journal_get_write_access(handle, bh);
  432. if (error)
  433. goto out;
  434. lock_buffer(bh);
  435. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  436. ea_bdebug(bh, "refcount now=0; freeing");
  437. if (ce)
  438. mb_cache_entry_free(ce);
  439. get_bh(bh);
  440. ext4_free_blocks(handle, inode, bh, 0, 1,
  441. EXT4_FREE_BLOCKS_METADATA |
  442. EXT4_FREE_BLOCKS_FORGET);
  443. } else {
  444. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  445. error = ext4_handle_dirty_metadata(handle, inode, bh);
  446. if (IS_SYNC(inode))
  447. ext4_handle_sync(handle);
  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_INODE(inode, "bad block %llu",
  606. EXT4_I(inode)->i_file_acl);
  607. error = -EIO;
  608. goto cleanup;
  609. }
  610. /* Find the named attribute. */
  611. bs->s.base = BHDR(bs->bh);
  612. bs->s.first = BFIRST(bs->bh);
  613. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  614. bs->s.here = bs->s.first;
  615. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  616. i->name, bs->bh->b_size, 1);
  617. if (error && error != -ENODATA)
  618. goto cleanup;
  619. bs->s.not_found = error;
  620. }
  621. error = 0;
  622. cleanup:
  623. return error;
  624. }
  625. static int
  626. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  627. struct ext4_xattr_info *i,
  628. struct ext4_xattr_block_find *bs)
  629. {
  630. struct super_block *sb = inode->i_sb;
  631. struct buffer_head *new_bh = NULL;
  632. struct ext4_xattr_search *s = &bs->s;
  633. struct mb_cache_entry *ce = NULL;
  634. int error = 0;
  635. #define header(x) ((struct ext4_xattr_header *)(x))
  636. if (i->value && i->value_len > sb->s_blocksize)
  637. return -ENOSPC;
  638. if (s->base) {
  639. ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
  640. bs->bh->b_blocknr);
  641. error = ext4_journal_get_write_access(handle, bs->bh);
  642. if (error)
  643. goto cleanup;
  644. lock_buffer(bs->bh);
  645. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  646. if (ce) {
  647. mb_cache_entry_free(ce);
  648. ce = NULL;
  649. }
  650. ea_bdebug(bs->bh, "modifying in-place");
  651. error = ext4_xattr_set_entry(i, s);
  652. if (!error) {
  653. if (!IS_LAST_ENTRY(s->first))
  654. ext4_xattr_rehash(header(s->base),
  655. s->here);
  656. ext4_xattr_cache_insert(bs->bh);
  657. }
  658. unlock_buffer(bs->bh);
  659. if (error == -EIO)
  660. goto bad_block;
  661. if (!error)
  662. error = ext4_handle_dirty_metadata(handle,
  663. inode,
  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. ext4_handle_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 = dquot_alloc_block(inode, 1);
  719. if (error)
  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_handle_dirty_metadata(handle,
  731. inode,
  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, block;
  746. goal = ext4_group_first_block_no(sb,
  747. EXT4_I(inode)->i_block_group);
  748. /* non-extent files can't have physical blocks past 2^32 */
  749. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  750. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  751. /*
  752. * take i_data_sem because we will test
  753. * i_delalloc_reserved_flag in ext4_mb_new_blocks
  754. */
  755. down_read((&EXT4_I(inode)->i_data_sem));
  756. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  757. NULL, &error);
  758. up_read((&EXT4_I(inode)->i_data_sem));
  759. if (error)
  760. goto cleanup;
  761. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  762. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  763. ea_idebug(inode, "creating block %d", block);
  764. new_bh = sb_getblk(sb, block);
  765. if (!new_bh) {
  766. getblk_failed:
  767. ext4_free_blocks(handle, inode, NULL, block, 1,
  768. EXT4_FREE_BLOCKS_METADATA);
  769. error = -EIO;
  770. goto cleanup;
  771. }
  772. lock_buffer(new_bh);
  773. error = ext4_journal_get_create_access(handle, new_bh);
  774. if (error) {
  775. unlock_buffer(new_bh);
  776. goto getblk_failed;
  777. }
  778. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  779. set_buffer_uptodate(new_bh);
  780. unlock_buffer(new_bh);
  781. ext4_xattr_cache_insert(new_bh);
  782. error = ext4_handle_dirty_metadata(handle,
  783. inode, new_bh);
  784. if (error)
  785. goto cleanup;
  786. }
  787. }
  788. /* Update the inode. */
  789. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  790. /* Drop the previous xattr block. */
  791. if (bs->bh && bs->bh != new_bh)
  792. ext4_xattr_release_block(handle, inode, bs->bh);
  793. error = 0;
  794. cleanup:
  795. if (ce)
  796. mb_cache_entry_release(ce);
  797. brelse(new_bh);
  798. if (!(bs->bh && s->base == bs->bh->b_data))
  799. kfree(s->base);
  800. return error;
  801. cleanup_dquot:
  802. dquot_free_block(inode, 1);
  803. goto cleanup;
  804. bad_block:
  805. EXT4_ERROR_INODE(inode, "bad block %llu",
  806. EXT4_I(inode)->i_file_acl);
  807. goto cleanup;
  808. #undef header
  809. }
  810. struct ext4_xattr_ibody_find {
  811. struct ext4_xattr_search s;
  812. struct ext4_iloc iloc;
  813. };
  814. static int
  815. ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  816. struct ext4_xattr_ibody_find *is)
  817. {
  818. struct ext4_xattr_ibody_header *header;
  819. struct ext4_inode *raw_inode;
  820. int error;
  821. if (EXT4_I(inode)->i_extra_isize == 0)
  822. return 0;
  823. raw_inode = ext4_raw_inode(&is->iloc);
  824. header = IHDR(inode, raw_inode);
  825. is->s.base = is->s.first = IFIRST(header);
  826. is->s.here = is->s.first;
  827. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  828. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  829. error = ext4_xattr_check_names(IFIRST(header), is->s.end);
  830. if (error)
  831. return error;
  832. /* Find the named attribute. */
  833. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  834. i->name, is->s.end -
  835. (void *)is->s.base, 0);
  836. if (error && error != -ENODATA)
  837. return error;
  838. is->s.not_found = error;
  839. }
  840. return 0;
  841. }
  842. static int
  843. ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  844. struct ext4_xattr_info *i,
  845. struct ext4_xattr_ibody_find *is)
  846. {
  847. struct ext4_xattr_ibody_header *header;
  848. struct ext4_xattr_search *s = &is->s;
  849. int error;
  850. if (EXT4_I(inode)->i_extra_isize == 0)
  851. return -ENOSPC;
  852. error = ext4_xattr_set_entry(i, s);
  853. if (error)
  854. return error;
  855. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  856. if (!IS_LAST_ENTRY(s->first)) {
  857. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  858. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  859. } else {
  860. header->h_magic = cpu_to_le32(0);
  861. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  862. }
  863. return 0;
  864. }
  865. /*
  866. * ext4_xattr_set_handle()
  867. *
  868. * Create, replace or remove an extended attribute for this inode. Value
  869. * is NULL to remove an existing extended attribute, and non-NULL to
  870. * either replace an existing extended attribute, or create a new extended
  871. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  872. * specify that an extended attribute must exist and must not exist
  873. * previous to the call, respectively.
  874. *
  875. * Returns 0, or a negative error number on failure.
  876. */
  877. int
  878. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  879. const char *name, const void *value, size_t value_len,
  880. int flags)
  881. {
  882. struct ext4_xattr_info i = {
  883. .name_index = name_index,
  884. .name = name,
  885. .value = value,
  886. .value_len = value_len,
  887. };
  888. struct ext4_xattr_ibody_find is = {
  889. .s = { .not_found = -ENODATA, },
  890. };
  891. struct ext4_xattr_block_find bs = {
  892. .s = { .not_found = -ENODATA, },
  893. };
  894. unsigned long no_expand;
  895. int error;
  896. if (!name)
  897. return -EINVAL;
  898. if (strlen(name) > 255)
  899. return -ERANGE;
  900. down_write(&EXT4_I(inode)->xattr_sem);
  901. no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
  902. ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
  903. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  904. if (error)
  905. goto cleanup;
  906. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  907. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  908. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  909. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  910. }
  911. error = ext4_xattr_ibody_find(inode, &i, &is);
  912. if (error)
  913. goto cleanup;
  914. if (is.s.not_found)
  915. error = ext4_xattr_block_find(inode, &i, &bs);
  916. if (error)
  917. goto cleanup;
  918. if (is.s.not_found && bs.s.not_found) {
  919. error = -ENODATA;
  920. if (flags & XATTR_REPLACE)
  921. goto cleanup;
  922. error = 0;
  923. if (!value)
  924. goto cleanup;
  925. } else {
  926. error = -EEXIST;
  927. if (flags & XATTR_CREATE)
  928. goto cleanup;
  929. }
  930. if (!value) {
  931. if (!is.s.not_found)
  932. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  933. else if (!bs.s.not_found)
  934. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  935. } else {
  936. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  937. if (!error && !bs.s.not_found) {
  938. i.value = NULL;
  939. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  940. } else if (error == -ENOSPC) {
  941. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  942. error = ext4_xattr_block_find(inode, &i, &bs);
  943. if (error)
  944. goto cleanup;
  945. }
  946. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  947. if (error)
  948. goto cleanup;
  949. if (!is.s.not_found) {
  950. i.value = NULL;
  951. error = ext4_xattr_ibody_set(handle, inode, &i,
  952. &is);
  953. }
  954. }
  955. }
  956. if (!error) {
  957. ext4_xattr_update_super_block(handle, inode->i_sb);
  958. inode->i_ctime = ext4_current_time(inode);
  959. if (!value)
  960. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  961. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  962. /*
  963. * The bh is consumed by ext4_mark_iloc_dirty, even with
  964. * error != 0.
  965. */
  966. is.iloc.bh = NULL;
  967. if (IS_SYNC(inode))
  968. ext4_handle_sync(handle);
  969. }
  970. cleanup:
  971. brelse(is.iloc.bh);
  972. brelse(bs.bh);
  973. if (no_expand == 0)
  974. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  975. up_write(&EXT4_I(inode)->xattr_sem);
  976. return error;
  977. }
  978. /*
  979. * ext4_xattr_set()
  980. *
  981. * Like ext4_xattr_set_handle, but start from an inode. This extended
  982. * attribute modification is a filesystem transaction by itself.
  983. *
  984. * Returns 0, or a negative error number on failure.
  985. */
  986. int
  987. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  988. const void *value, size_t value_len, int flags)
  989. {
  990. handle_t *handle;
  991. int error, retries = 0;
  992. retry:
  993. handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  994. if (IS_ERR(handle)) {
  995. error = PTR_ERR(handle);
  996. } else {
  997. int error2;
  998. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  999. value, value_len, flags);
  1000. error2 = ext4_journal_stop(handle);
  1001. if (error == -ENOSPC &&
  1002. ext4_should_retry_alloc(inode->i_sb, &retries))
  1003. goto retry;
  1004. if (error == 0)
  1005. error = error2;
  1006. }
  1007. return error;
  1008. }
  1009. /*
  1010. * Shift the EA entries in the inode to create space for the increased
  1011. * i_extra_isize.
  1012. */
  1013. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  1014. int value_offs_shift, void *to,
  1015. void *from, size_t n, int blocksize)
  1016. {
  1017. struct ext4_xattr_entry *last = entry;
  1018. int new_offs;
  1019. /* Adjust the value offsets of the entries */
  1020. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1021. if (!last->e_value_block && last->e_value_size) {
  1022. new_offs = le16_to_cpu(last->e_value_offs) +
  1023. value_offs_shift;
  1024. BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
  1025. > blocksize);
  1026. last->e_value_offs = cpu_to_le16(new_offs);
  1027. }
  1028. }
  1029. /* Shift the entries by n bytes */
  1030. memmove(to, from, n);
  1031. }
  1032. /*
  1033. * Expand an inode by new_extra_isize bytes when EAs are present.
  1034. * Returns 0 on success or negative error number on failure.
  1035. */
  1036. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1037. struct ext4_inode *raw_inode, handle_t *handle)
  1038. {
  1039. struct ext4_xattr_ibody_header *header;
  1040. struct ext4_xattr_entry *entry, *last, *first;
  1041. struct buffer_head *bh = NULL;
  1042. struct ext4_xattr_ibody_find *is = NULL;
  1043. struct ext4_xattr_block_find *bs = NULL;
  1044. char *buffer = NULL, *b_entry_name = NULL;
  1045. size_t min_offs, free;
  1046. int total_ino, total_blk;
  1047. void *base, *start, *end;
  1048. int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
  1049. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1050. down_write(&EXT4_I(inode)->xattr_sem);
  1051. retry:
  1052. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
  1053. up_write(&EXT4_I(inode)->xattr_sem);
  1054. return 0;
  1055. }
  1056. header = IHDR(inode, raw_inode);
  1057. entry = IFIRST(header);
  1058. /*
  1059. * Check if enough free space is available in the inode to shift the
  1060. * entries ahead by new_extra_isize.
  1061. */
  1062. base = start = entry;
  1063. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1064. min_offs = end - base;
  1065. last = entry;
  1066. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1067. free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
  1068. if (free >= new_extra_isize) {
  1069. entry = IFIRST(header);
  1070. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1071. - new_extra_isize, (void *)raw_inode +
  1072. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1073. (void *)header, total_ino,
  1074. inode->i_sb->s_blocksize);
  1075. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1076. error = 0;
  1077. goto cleanup;
  1078. }
  1079. /*
  1080. * Enough free space isn't available in the inode, check if
  1081. * EA block can hold new_extra_isize bytes.
  1082. */
  1083. if (EXT4_I(inode)->i_file_acl) {
  1084. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1085. error = -EIO;
  1086. if (!bh)
  1087. goto cleanup;
  1088. if (ext4_xattr_check_block(bh)) {
  1089. EXT4_ERROR_INODE(inode, "bad block %llu",
  1090. EXT4_I(inode)->i_file_acl);
  1091. error = -EIO;
  1092. goto cleanup;
  1093. }
  1094. base = BHDR(bh);
  1095. first = BFIRST(bh);
  1096. end = bh->b_data + bh->b_size;
  1097. min_offs = end - base;
  1098. free = ext4_xattr_free_space(first, &min_offs, base,
  1099. &total_blk);
  1100. if (free < new_extra_isize) {
  1101. if (!tried_min_extra_isize && s_min_extra_isize) {
  1102. tried_min_extra_isize++;
  1103. new_extra_isize = s_min_extra_isize;
  1104. brelse(bh);
  1105. goto retry;
  1106. }
  1107. error = -1;
  1108. goto cleanup;
  1109. }
  1110. } else {
  1111. free = inode->i_sb->s_blocksize;
  1112. }
  1113. while (new_extra_isize > 0) {
  1114. size_t offs, size, entry_size;
  1115. struct ext4_xattr_entry *small_entry = NULL;
  1116. struct ext4_xattr_info i = {
  1117. .value = NULL,
  1118. .value_len = 0,
  1119. };
  1120. unsigned int total_size; /* EA entry size + value size */
  1121. unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
  1122. unsigned int min_total_size = ~0U;
  1123. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1124. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1125. if (!is || !bs) {
  1126. error = -ENOMEM;
  1127. goto cleanup;
  1128. }
  1129. is->s.not_found = -ENODATA;
  1130. bs->s.not_found = -ENODATA;
  1131. is->iloc.bh = NULL;
  1132. bs->bh = NULL;
  1133. last = IFIRST(header);
  1134. /* Find the entry best suited to be pushed into EA block */
  1135. entry = NULL;
  1136. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1137. total_size =
  1138. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1139. EXT4_XATTR_LEN(last->e_name_len);
  1140. if (total_size <= free && total_size < min_total_size) {
  1141. if (total_size < new_extra_isize) {
  1142. small_entry = last;
  1143. } else {
  1144. entry = last;
  1145. min_total_size = total_size;
  1146. }
  1147. }
  1148. }
  1149. if (entry == NULL) {
  1150. if (small_entry) {
  1151. entry = small_entry;
  1152. } else {
  1153. if (!tried_min_extra_isize &&
  1154. s_min_extra_isize) {
  1155. tried_min_extra_isize++;
  1156. new_extra_isize = s_min_extra_isize;
  1157. goto retry;
  1158. }
  1159. error = -1;
  1160. goto cleanup;
  1161. }
  1162. }
  1163. offs = le16_to_cpu(entry->e_value_offs);
  1164. size = le32_to_cpu(entry->e_value_size);
  1165. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1166. i.name_index = entry->e_name_index,
  1167. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
  1168. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1169. if (!buffer || !b_entry_name) {
  1170. error = -ENOMEM;
  1171. goto cleanup;
  1172. }
  1173. /* Save the entry name and the entry value */
  1174. memcpy(buffer, (void *)IFIRST(header) + offs,
  1175. EXT4_XATTR_SIZE(size));
  1176. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1177. b_entry_name[entry->e_name_len] = '\0';
  1178. i.name = b_entry_name;
  1179. error = ext4_get_inode_loc(inode, &is->iloc);
  1180. if (error)
  1181. goto cleanup;
  1182. error = ext4_xattr_ibody_find(inode, &i, is);
  1183. if (error)
  1184. goto cleanup;
  1185. /* Remove the chosen entry from the inode */
  1186. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1187. if (error)
  1188. goto cleanup;
  1189. entry = IFIRST(header);
  1190. if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
  1191. shift_bytes = new_extra_isize;
  1192. else
  1193. shift_bytes = entry_size + size;
  1194. /* Adjust the offsets and shift the remaining entries ahead */
  1195. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
  1196. shift_bytes, (void *)raw_inode +
  1197. EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
  1198. (void *)header, total_ino - entry_size,
  1199. inode->i_sb->s_blocksize);
  1200. extra_isize += shift_bytes;
  1201. new_extra_isize -= shift_bytes;
  1202. EXT4_I(inode)->i_extra_isize = extra_isize;
  1203. i.name = b_entry_name;
  1204. i.value = buffer;
  1205. i.value_len = size;
  1206. error = ext4_xattr_block_find(inode, &i, bs);
  1207. if (error)
  1208. goto cleanup;
  1209. /* Add entry which was removed from the inode into the block */
  1210. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1211. if (error)
  1212. goto cleanup;
  1213. kfree(b_entry_name);
  1214. kfree(buffer);
  1215. b_entry_name = NULL;
  1216. buffer = NULL;
  1217. brelse(is->iloc.bh);
  1218. kfree(is);
  1219. kfree(bs);
  1220. }
  1221. brelse(bh);
  1222. up_write(&EXT4_I(inode)->xattr_sem);
  1223. return 0;
  1224. cleanup:
  1225. kfree(b_entry_name);
  1226. kfree(buffer);
  1227. if (is)
  1228. brelse(is->iloc.bh);
  1229. kfree(is);
  1230. kfree(bs);
  1231. brelse(bh);
  1232. up_write(&EXT4_I(inode)->xattr_sem);
  1233. return error;
  1234. }
  1235. /*
  1236. * ext4_xattr_delete_inode()
  1237. *
  1238. * Free extended attribute resources associated with this inode. This
  1239. * is called immediately before an inode is freed. We have exclusive
  1240. * access to the inode.
  1241. */
  1242. void
  1243. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1244. {
  1245. struct buffer_head *bh = NULL;
  1246. if (!EXT4_I(inode)->i_file_acl)
  1247. goto cleanup;
  1248. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1249. if (!bh) {
  1250. EXT4_ERROR_INODE(inode, "block %llu read error",
  1251. EXT4_I(inode)->i_file_acl);
  1252. goto cleanup;
  1253. }
  1254. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1255. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1256. EXT4_ERROR_INODE(inode, "bad block %llu",
  1257. EXT4_I(inode)->i_file_acl);
  1258. goto cleanup;
  1259. }
  1260. ext4_xattr_release_block(handle, inode, bh);
  1261. EXT4_I(inode)->i_file_acl = 0;
  1262. cleanup:
  1263. brelse(bh);
  1264. }
  1265. /*
  1266. * ext4_xattr_put_super()
  1267. *
  1268. * This is called when a file system is unmounted.
  1269. */
  1270. void
  1271. ext4_xattr_put_super(struct super_block *sb)
  1272. {
  1273. mb_cache_shrink(sb->s_bdev);
  1274. }
  1275. /*
  1276. * ext4_xattr_cache_insert()
  1277. *
  1278. * Create a new entry in the extended attribute cache, and insert
  1279. * it unless such an entry is already in the cache.
  1280. *
  1281. * Returns 0, or a negative error number on failure.
  1282. */
  1283. static void
  1284. ext4_xattr_cache_insert(struct buffer_head *bh)
  1285. {
  1286. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1287. struct mb_cache_entry *ce;
  1288. int error;
  1289. ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
  1290. if (!ce) {
  1291. ea_bdebug(bh, "out of memory");
  1292. return;
  1293. }
  1294. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
  1295. if (error) {
  1296. mb_cache_entry_free(ce);
  1297. if (error == -EBUSY) {
  1298. ea_bdebug(bh, "already in cache");
  1299. error = 0;
  1300. }
  1301. } else {
  1302. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1303. mb_cache_entry_release(ce);
  1304. }
  1305. }
  1306. /*
  1307. * ext4_xattr_cmp()
  1308. *
  1309. * Compare two extended attribute blocks for equality.
  1310. *
  1311. * Returns 0 if the blocks are equal, 1 if they differ, and
  1312. * a negative error number on errors.
  1313. */
  1314. static int
  1315. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1316. struct ext4_xattr_header *header2)
  1317. {
  1318. struct ext4_xattr_entry *entry1, *entry2;
  1319. entry1 = ENTRY(header1+1);
  1320. entry2 = ENTRY(header2+1);
  1321. while (!IS_LAST_ENTRY(entry1)) {
  1322. if (IS_LAST_ENTRY(entry2))
  1323. return 1;
  1324. if (entry1->e_hash != entry2->e_hash ||
  1325. entry1->e_name_index != entry2->e_name_index ||
  1326. entry1->e_name_len != entry2->e_name_len ||
  1327. entry1->e_value_size != entry2->e_value_size ||
  1328. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1329. return 1;
  1330. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1331. return -EIO;
  1332. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1333. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1334. le32_to_cpu(entry1->e_value_size)))
  1335. return 1;
  1336. entry1 = EXT4_XATTR_NEXT(entry1);
  1337. entry2 = EXT4_XATTR_NEXT(entry2);
  1338. }
  1339. if (!IS_LAST_ENTRY(entry2))
  1340. return 1;
  1341. return 0;
  1342. }
  1343. /*
  1344. * ext4_xattr_cache_find()
  1345. *
  1346. * Find an identical extended attribute block.
  1347. *
  1348. * Returns a pointer to the block found, or NULL if such a block was
  1349. * not found or an error occurred.
  1350. */
  1351. static struct buffer_head *
  1352. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1353. struct mb_cache_entry **pce)
  1354. {
  1355. __u32 hash = le32_to_cpu(header->h_hash);
  1356. struct mb_cache_entry *ce;
  1357. if (!header->h_hash)
  1358. return NULL; /* never share */
  1359. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1360. again:
  1361. ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
  1362. hash);
  1363. while (ce) {
  1364. struct buffer_head *bh;
  1365. if (IS_ERR(ce)) {
  1366. if (PTR_ERR(ce) == -EAGAIN)
  1367. goto again;
  1368. break;
  1369. }
  1370. bh = sb_bread(inode->i_sb, ce->e_block);
  1371. if (!bh) {
  1372. EXT4_ERROR_INODE(inode, "block %lu read error",
  1373. (unsigned long) ce->e_block);
  1374. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1375. EXT4_XATTR_REFCOUNT_MAX) {
  1376. ea_idebug(inode, "block %lu refcount %d>=%d",
  1377. (unsigned long) ce->e_block,
  1378. le32_to_cpu(BHDR(bh)->h_refcount),
  1379. EXT4_XATTR_REFCOUNT_MAX);
  1380. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1381. *pce = ce;
  1382. return bh;
  1383. }
  1384. brelse(bh);
  1385. ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
  1386. }
  1387. return NULL;
  1388. }
  1389. #define NAME_HASH_SHIFT 5
  1390. #define VALUE_HASH_SHIFT 16
  1391. /*
  1392. * ext4_xattr_hash_entry()
  1393. *
  1394. * Compute the hash of an extended attribute.
  1395. */
  1396. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1397. struct ext4_xattr_entry *entry)
  1398. {
  1399. __u32 hash = 0;
  1400. char *name = entry->e_name;
  1401. int n;
  1402. for (n = 0; n < entry->e_name_len; n++) {
  1403. hash = (hash << NAME_HASH_SHIFT) ^
  1404. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1405. *name++;
  1406. }
  1407. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1408. __le32 *value = (__le32 *)((char *)header +
  1409. le16_to_cpu(entry->e_value_offs));
  1410. for (n = (le32_to_cpu(entry->e_value_size) +
  1411. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1412. hash = (hash << VALUE_HASH_SHIFT) ^
  1413. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1414. le32_to_cpu(*value++);
  1415. }
  1416. }
  1417. entry->e_hash = cpu_to_le32(hash);
  1418. }
  1419. #undef NAME_HASH_SHIFT
  1420. #undef VALUE_HASH_SHIFT
  1421. #define BLOCK_HASH_SHIFT 16
  1422. /*
  1423. * ext4_xattr_rehash()
  1424. *
  1425. * Re-compute the extended attribute hash value after an entry has changed.
  1426. */
  1427. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1428. struct ext4_xattr_entry *entry)
  1429. {
  1430. struct ext4_xattr_entry *here;
  1431. __u32 hash = 0;
  1432. ext4_xattr_hash_entry(header, entry);
  1433. here = ENTRY(header+1);
  1434. while (!IS_LAST_ENTRY(here)) {
  1435. if (!here->e_hash) {
  1436. /* Block is not shared if an entry's hash value == 0 */
  1437. hash = 0;
  1438. break;
  1439. }
  1440. hash = (hash << BLOCK_HASH_SHIFT) ^
  1441. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1442. le32_to_cpu(here->e_hash);
  1443. here = EXT4_XATTR_NEXT(here);
  1444. }
  1445. header->h_hash = cpu_to_le32(hash);
  1446. }
  1447. #undef BLOCK_HASH_SHIFT
  1448. int __init
  1449. ext4_init_xattr(void)
  1450. {
  1451. ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
  1452. if (!ext4_xattr_cache)
  1453. return -ENOMEM;
  1454. return 0;
  1455. }
  1456. void
  1457. ext4_exit_xattr(void)
  1458. {
  1459. if (ext4_xattr_cache)
  1460. mb_cache_destroy(ext4_xattr_cache);
  1461. ext4_xattr_cache = NULL;
  1462. }