xattr.c 42 KB

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