xattr.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /*
  2. * linux/fs/reiserfs/xattr.c
  3. *
  4. * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
  5. *
  6. */
  7. /*
  8. * In order to implement EA/ACLs in a clean, backwards compatible manner,
  9. * they are implemented as files in a "private" directory.
  10. * Each EA is in it's own file, with the directory layout like so (/ is assumed
  11. * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
  12. * directories named using the capital-hex form of the objectid and
  13. * generation number are used. Inside each directory are individual files
  14. * named with the name of the extended attribute.
  15. *
  16. * So, for objectid 12648430, we could have:
  17. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
  18. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
  19. * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
  20. * .. or similar.
  21. *
  22. * The file contents are the text of the EA. The size is known based on the
  23. * stat data describing the file.
  24. *
  25. * In the case of system.posix_acl_access and system.posix_acl_default, since
  26. * these are special cases for filesystem ACLs, they are interpreted by the
  27. * kernel, in addition, they are negatively and positively cached and attached
  28. * to the inode so that unnecessary lookups are avoided.
  29. */
  30. #include <linux/reiserfs_fs.h>
  31. #include <linux/capability.h>
  32. #include <linux/dcache.h>
  33. #include <linux/namei.h>
  34. #include <linux/errno.h>
  35. #include <linux/fs.h>
  36. #include <linux/file.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/xattr.h>
  39. #include <linux/reiserfs_xattr.h>
  40. #include <linux/reiserfs_acl.h>
  41. #include <asm/uaccess.h>
  42. #include <net/checksum.h>
  43. #include <linux/smp_lock.h>
  44. #include <linux/stat.h>
  45. #define FL_READONLY 128
  46. #define FL_DIR_SEM_HELD 256
  47. #define PRIVROOT_NAME ".reiserfs_priv"
  48. #define XAROOT_NAME "xattrs"
  49. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  50. *prefix);
  51. /* Returns the dentry referring to the root of the extended attribute
  52. * directory tree. If it has already been retrieved, it is used. If it
  53. * hasn't been created and the flags indicate creation is allowed, we
  54. * attempt to create it. On error, we return a pointer-encoded error.
  55. */
  56. static struct dentry *get_xa_root(struct super_block *sb, int flags)
  57. {
  58. struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
  59. struct dentry *xaroot;
  60. /* This needs to be created at mount-time */
  61. if (!privroot)
  62. return ERR_PTR(-ENODATA);
  63. mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
  64. if (REISERFS_SB(sb)->xattr_root) {
  65. xaroot = dget(REISERFS_SB(sb)->xattr_root);
  66. goto out;
  67. }
  68. xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
  69. if (IS_ERR(xaroot)) {
  70. goto out;
  71. } else if (!xaroot->d_inode) {
  72. int err = -ENODATA;
  73. if (flags == 0 || flags & XATTR_CREATE)
  74. err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
  75. xaroot, 0700);
  76. if (err) {
  77. dput(xaroot);
  78. xaroot = ERR_PTR(err);
  79. goto out;
  80. }
  81. }
  82. REISERFS_SB(sb)->xattr_root = dget(xaroot);
  83. out:
  84. mutex_unlock(&privroot->d_inode->i_mutex);
  85. dput(privroot);
  86. return xaroot;
  87. }
  88. /* Opens the directory corresponding to the inode's extended attribute store.
  89. * If flags allow, the tree to the directory may be created. If creation is
  90. * prohibited, -ENODATA is returned. */
  91. static struct dentry *open_xa_dir(const struct inode *inode, int flags)
  92. {
  93. struct dentry *xaroot, *xadir;
  94. char namebuf[17];
  95. xaroot = get_xa_root(inode->i_sb, flags);
  96. if (IS_ERR(xaroot))
  97. return xaroot;
  98. /* ok, we have xaroot open */
  99. snprintf(namebuf, sizeof(namebuf), "%X.%X",
  100. le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  101. inode->i_generation);
  102. xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
  103. if (IS_ERR(xadir)) {
  104. dput(xaroot);
  105. return xadir;
  106. }
  107. if (!xadir->d_inode) {
  108. int err;
  109. if (flags == 0 || flags & XATTR_CREATE) {
  110. /* Although there is nothing else trying to create this directory,
  111. * another directory with the same hash may be created, so we need
  112. * to protect against that */
  113. err =
  114. xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
  115. 0700);
  116. if (err) {
  117. dput(xaroot);
  118. dput(xadir);
  119. return ERR_PTR(err);
  120. }
  121. }
  122. if (!xadir->d_inode) {
  123. dput(xaroot);
  124. dput(xadir);
  125. return ERR_PTR(-ENODATA);
  126. }
  127. }
  128. dput(xaroot);
  129. return xadir;
  130. }
  131. /* Returns a dentry corresponding to a specific extended attribute file
  132. * for the inode. If flags allow, the file is created. Otherwise, a
  133. * valid or negative dentry, or an error is returned. */
  134. static struct dentry *get_xa_file_dentry(const struct inode *inode,
  135. const char *name, int flags)
  136. {
  137. struct dentry *xadir, *xafile;
  138. int err = 0;
  139. xadir = open_xa_dir(inode, flags);
  140. if (IS_ERR(xadir)) {
  141. return ERR_CAST(xadir);
  142. } else if (!xadir->d_inode) {
  143. dput(xadir);
  144. return ERR_PTR(-ENODATA);
  145. }
  146. xafile = lookup_one_len(name, xadir, strlen(name));
  147. if (IS_ERR(xafile)) {
  148. dput(xadir);
  149. return ERR_CAST(xafile);
  150. }
  151. if (xafile->d_inode) { /* file exists */
  152. if (flags & XATTR_CREATE) {
  153. err = -EEXIST;
  154. dput(xafile);
  155. goto out;
  156. }
  157. } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
  158. goto out;
  159. } else {
  160. /* inode->i_mutex is down, so nothing else can try to create
  161. * the same xattr */
  162. err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
  163. 0700 | S_IFREG, NULL);
  164. if (err) {
  165. dput(xafile);
  166. goto out;
  167. }
  168. }
  169. out:
  170. dput(xadir);
  171. if (err)
  172. xafile = ERR_PTR(err);
  173. else if (!xafile->d_inode) {
  174. dput(xafile);
  175. xafile = ERR_PTR(-ENODATA);
  176. }
  177. return xafile;
  178. }
  179. /*
  180. * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
  181. * we need to drop the path before calling the filldir struct. That
  182. * would be a big performance hit to the non-xattr case, so I've copied
  183. * the whole thing for now. --clm
  184. *
  185. * the big difference is that I go backwards through the directory,
  186. * and don't mess with f->f_pos, but the idea is the same. Do some
  187. * action on each and every entry in the directory.
  188. *
  189. * we're called with i_mutex held, so there are no worries about the directory
  190. * changing underneath us.
  191. */
  192. static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
  193. {
  194. struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
  195. INITIALIZE_PATH(path_to_entry);
  196. struct buffer_head *bh;
  197. int entry_num;
  198. struct item_head *ih, tmp_ih;
  199. int search_res;
  200. char *local_buf;
  201. loff_t next_pos;
  202. char small_buf[32]; /* avoid kmalloc if we can */
  203. struct reiserfs_de_head *deh;
  204. int d_reclen;
  205. char *d_name;
  206. off_t d_off;
  207. ino_t d_ino;
  208. struct reiserfs_dir_entry de;
  209. /* form key for search the next directory entry using f_pos field of
  210. file structure */
  211. next_pos = max_reiserfs_offset(inode);
  212. while (1) {
  213. research:
  214. if (next_pos <= DOT_DOT_OFFSET)
  215. break;
  216. make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
  217. search_res =
  218. search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
  219. &de);
  220. if (search_res == IO_ERROR) {
  221. // FIXME: we could just skip part of directory which could
  222. // not be read
  223. pathrelse(&path_to_entry);
  224. return -EIO;
  225. }
  226. if (search_res == NAME_NOT_FOUND)
  227. de.de_entry_num--;
  228. set_de_name_and_namelen(&de);
  229. entry_num = de.de_entry_num;
  230. deh = &(de.de_deh[entry_num]);
  231. bh = de.de_bh;
  232. ih = de.de_ih;
  233. if (!is_direntry_le_ih(ih)) {
  234. reiserfs_error(inode->i_sb, "jdm-20000",
  235. "not direntry %h", ih);
  236. break;
  237. }
  238. copy_item_head(&tmp_ih, ih);
  239. /* we must have found item, that is item of this directory, */
  240. RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
  241. "vs-9000: found item %h does not match to dir we readdir %K",
  242. ih, &pos_key);
  243. if (deh_offset(deh) <= DOT_DOT_OFFSET) {
  244. break;
  245. }
  246. /* look for the previous entry in the directory */
  247. next_pos = deh_offset(deh) - 1;
  248. if (!de_visible(deh))
  249. /* it is hidden entry */
  250. continue;
  251. d_reclen = entry_length(bh, ih, entry_num);
  252. d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
  253. d_off = deh_offset(deh);
  254. d_ino = deh_objectid(deh);
  255. if (!d_name[d_reclen - 1])
  256. d_reclen = strlen(d_name);
  257. if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
  258. /* too big to send back to VFS */
  259. continue;
  260. }
  261. /* Ignore the .reiserfs_priv entry */
  262. if (reiserfs_xattrs(inode->i_sb) &&
  263. !old_format_only(inode->i_sb) &&
  264. deh_objectid(deh) ==
  265. le32_to_cpu(INODE_PKEY
  266. (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
  267. k_objectid))
  268. continue;
  269. if (d_reclen <= 32) {
  270. local_buf = small_buf;
  271. } else {
  272. local_buf = kmalloc(d_reclen, GFP_NOFS);
  273. if (!local_buf) {
  274. pathrelse(&path_to_entry);
  275. return -ENOMEM;
  276. }
  277. if (item_moved(&tmp_ih, &path_to_entry)) {
  278. kfree(local_buf);
  279. /* sigh, must retry. Do this same offset again */
  280. next_pos = d_off;
  281. goto research;
  282. }
  283. }
  284. // Note, that we copy name to user space via temporary
  285. // buffer (local_buf) because filldir will block if
  286. // user space buffer is swapped out. At that time
  287. // entry can move to somewhere else
  288. memcpy(local_buf, d_name, d_reclen);
  289. /* the filldir function might need to start transactions,
  290. * or do who knows what. Release the path now that we've
  291. * copied all the important stuff out of the deh
  292. */
  293. pathrelse(&path_to_entry);
  294. if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
  295. DT_UNKNOWN) < 0) {
  296. if (local_buf != small_buf) {
  297. kfree(local_buf);
  298. }
  299. goto end;
  300. }
  301. if (local_buf != small_buf) {
  302. kfree(local_buf);
  303. }
  304. } /* while */
  305. end:
  306. pathrelse(&path_to_entry);
  307. return 0;
  308. }
  309. /*
  310. * this could be done with dedicated readdir ops for the xattr files,
  311. * but I want to get something working asap
  312. * this is stolen from vfs_readdir
  313. *
  314. */
  315. static
  316. int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
  317. {
  318. int res = -ENOENT;
  319. mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
  320. if (!IS_DEADDIR(inode)) {
  321. lock_kernel();
  322. res = __xattr_readdir(inode, buf, filler);
  323. unlock_kernel();
  324. }
  325. mutex_unlock(&inode->i_mutex);
  326. return res;
  327. }
  328. /* Internal operations on file data */
  329. static inline void reiserfs_put_page(struct page *page)
  330. {
  331. kunmap(page);
  332. page_cache_release(page);
  333. }
  334. static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
  335. {
  336. struct address_space *mapping = dir->i_mapping;
  337. struct page *page;
  338. /* We can deadlock if we try to free dentries,
  339. and an unlink/rmdir has just occured - GFP_NOFS avoids this */
  340. mapping_set_gfp_mask(mapping, GFP_NOFS);
  341. page = read_mapping_page(mapping, n, NULL);
  342. if (!IS_ERR(page)) {
  343. kmap(page);
  344. if (PageError(page))
  345. goto fail;
  346. }
  347. return page;
  348. fail:
  349. reiserfs_put_page(page);
  350. return ERR_PTR(-EIO);
  351. }
  352. static inline __u32 xattr_hash(const char *msg, int len)
  353. {
  354. return csum_partial(msg, len, 0);
  355. }
  356. int reiserfs_commit_write(struct file *f, struct page *page,
  357. unsigned from, unsigned to);
  358. int reiserfs_prepare_write(struct file *f, struct page *page,
  359. unsigned from, unsigned to);
  360. /* Generic extended attribute operations that can be used by xa plugins */
  361. /*
  362. * inode->i_mutex: down
  363. */
  364. int
  365. reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
  366. size_t buffer_size, int flags)
  367. {
  368. int err = 0;
  369. struct dentry *dentry;
  370. struct page *page;
  371. char *data;
  372. struct address_space *mapping;
  373. size_t file_pos = 0;
  374. size_t buffer_pos = 0;
  375. struct inode *xinode;
  376. struct iattr newattrs;
  377. __u32 xahash = 0;
  378. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  379. return -EOPNOTSUPP;
  380. /* Empty xattrs are ok, they're just empty files, no hash */
  381. if (buffer && buffer_size)
  382. xahash = xattr_hash(buffer, buffer_size);
  383. open_file:
  384. dentry = get_xa_file_dentry(inode, name, flags);
  385. if (IS_ERR(dentry)) {
  386. err = PTR_ERR(dentry);
  387. goto out;
  388. }
  389. xinode = dentry->d_inode;
  390. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  391. /* we need to copy it off.. */
  392. if (xinode->i_nlink > 1) {
  393. dput(dentry);
  394. err = reiserfs_xattr_del(inode, name);
  395. if (err < 0)
  396. goto out;
  397. /* We just killed the old one, we're not replacing anymore */
  398. if (flags & XATTR_REPLACE)
  399. flags &= ~XATTR_REPLACE;
  400. goto open_file;
  401. }
  402. /* Resize it so we're ok to write there */
  403. newattrs.ia_size = buffer_size;
  404. newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
  405. mutex_lock_nested(&xinode->i_mutex, I_MUTEX_XATTR);
  406. err = notify_change(dentry, &newattrs);
  407. if (err)
  408. goto out_filp;
  409. mapping = xinode->i_mapping;
  410. while (buffer_pos < buffer_size || buffer_pos == 0) {
  411. size_t chunk;
  412. size_t skip = 0;
  413. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  414. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  415. chunk = PAGE_CACHE_SIZE;
  416. else
  417. chunk = buffer_size - buffer_pos;
  418. page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
  419. if (IS_ERR(page)) {
  420. err = PTR_ERR(page);
  421. goto out_filp;
  422. }
  423. lock_page(page);
  424. data = page_address(page);
  425. if (file_pos == 0) {
  426. struct reiserfs_xattr_header *rxh;
  427. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  428. if (chunk + skip > PAGE_CACHE_SIZE)
  429. chunk = PAGE_CACHE_SIZE - skip;
  430. rxh = (struct reiserfs_xattr_header *)data;
  431. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  432. rxh->h_hash = cpu_to_le32(xahash);
  433. }
  434. err = reiserfs_prepare_write(NULL, page, page_offset,
  435. page_offset + chunk + skip);
  436. if (!err) {
  437. if (buffer)
  438. memcpy(data + skip, buffer + buffer_pos, chunk);
  439. err = reiserfs_commit_write(NULL, page, page_offset,
  440. page_offset + chunk +
  441. skip);
  442. }
  443. unlock_page(page);
  444. reiserfs_put_page(page);
  445. buffer_pos += chunk;
  446. file_pos += chunk;
  447. skip = 0;
  448. if (err || buffer_size == 0 || !buffer)
  449. break;
  450. }
  451. /* We can't mark the inode dirty if it's not hashed. This is the case
  452. * when we're inheriting the default ACL. If we dirty it, the inode
  453. * gets marked dirty, but won't (ever) make it onto the dirty list until
  454. * it's synced explicitly to clear I_DIRTY. This is bad. */
  455. if (!hlist_unhashed(&inode->i_hash)) {
  456. inode->i_ctime = CURRENT_TIME_SEC;
  457. mark_inode_dirty(inode);
  458. }
  459. out_filp:
  460. mutex_unlock(&xinode->i_mutex);
  461. dput(dentry);
  462. out:
  463. return err;
  464. }
  465. /*
  466. * inode->i_mutex: down
  467. */
  468. int
  469. reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
  470. size_t buffer_size)
  471. {
  472. ssize_t err = 0;
  473. struct dentry *dentry;
  474. size_t isize;
  475. size_t file_pos = 0;
  476. size_t buffer_pos = 0;
  477. struct page *page;
  478. struct inode *xinode;
  479. __u32 hash = 0;
  480. if (name == NULL)
  481. return -EINVAL;
  482. /* We can't have xattrs attached to v1 items since they don't have
  483. * generation numbers */
  484. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  485. return -EOPNOTSUPP;
  486. dentry = get_xa_file_dentry(inode, name, FL_READONLY);
  487. if (IS_ERR(dentry)) {
  488. err = PTR_ERR(dentry);
  489. goto out;
  490. }
  491. xinode = dentry->d_inode;
  492. isize = xinode->i_size;
  493. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  494. /* Just return the size needed */
  495. if (buffer == NULL) {
  496. err = isize - sizeof(struct reiserfs_xattr_header);
  497. goto out_dput;
  498. }
  499. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  500. err = -ERANGE;
  501. goto out_dput;
  502. }
  503. while (file_pos < isize) {
  504. size_t chunk;
  505. char *data;
  506. size_t skip = 0;
  507. if (isize - file_pos > PAGE_CACHE_SIZE)
  508. chunk = PAGE_CACHE_SIZE;
  509. else
  510. chunk = isize - file_pos;
  511. page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
  512. if (IS_ERR(page)) {
  513. err = PTR_ERR(page);
  514. goto out_dput;
  515. }
  516. lock_page(page);
  517. data = page_address(page);
  518. if (file_pos == 0) {
  519. struct reiserfs_xattr_header *rxh =
  520. (struct reiserfs_xattr_header *)data;
  521. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  522. chunk -= skip;
  523. /* Magic doesn't match up.. */
  524. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  525. unlock_page(page);
  526. reiserfs_put_page(page);
  527. reiserfs_warning(inode->i_sb, "jdm-20001",
  528. "Invalid magic for xattr (%s) "
  529. "associated with %k", name,
  530. INODE_PKEY(inode));
  531. err = -EIO;
  532. goto out_dput;
  533. }
  534. hash = le32_to_cpu(rxh->h_hash);
  535. }
  536. memcpy(buffer + buffer_pos, data + skip, chunk);
  537. unlock_page(page);
  538. reiserfs_put_page(page);
  539. file_pos += chunk;
  540. buffer_pos += chunk;
  541. skip = 0;
  542. }
  543. err = isize - sizeof(struct reiserfs_xattr_header);
  544. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  545. hash) {
  546. reiserfs_warning(inode->i_sb, "jdm-20002",
  547. "Invalid hash for xattr (%s) associated "
  548. "with %k", name, INODE_PKEY(inode));
  549. err = -EIO;
  550. }
  551. out_dput:
  552. dput(dentry);
  553. out:
  554. return err;
  555. }
  556. static int
  557. __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
  558. {
  559. struct dentry *dentry;
  560. struct inode *dir = xadir->d_inode;
  561. int err = 0;
  562. dentry = lookup_one_len(name, xadir, namelen);
  563. if (IS_ERR(dentry)) {
  564. err = PTR_ERR(dentry);
  565. goto out;
  566. } else if (!dentry->d_inode) {
  567. err = -ENODATA;
  568. goto out_file;
  569. }
  570. /* Skip directories.. */
  571. if (S_ISDIR(dentry->d_inode->i_mode))
  572. goto out_file;
  573. if (!is_reiserfs_priv_object(dentry->d_inode)) {
  574. reiserfs_error(dir->i_sb, "jdm-20003",
  575. "OID %08x [%.*s/%.*s] doesn't have "
  576. "priv flag set [parent is %sset].",
  577. le32_to_cpu(INODE_PKEY(dentry->d_inode)->
  578. k_objectid), xadir->d_name.len,
  579. xadir->d_name.name, namelen, name,
  580. is_reiserfs_priv_object(xadir->d_inode) ? "" :
  581. "not ");
  582. dput(dentry);
  583. return -EIO;
  584. }
  585. err = dir->i_op->unlink(dir, dentry);
  586. if (!err)
  587. d_delete(dentry);
  588. out_file:
  589. dput(dentry);
  590. out:
  591. return err;
  592. }
  593. int reiserfs_xattr_del(struct inode *inode, const char *name)
  594. {
  595. struct dentry *dir;
  596. int err;
  597. dir = open_xa_dir(inode, FL_READONLY);
  598. if (IS_ERR(dir)) {
  599. err = PTR_ERR(dir);
  600. goto out;
  601. }
  602. err = __reiserfs_xattr_del(dir, name, strlen(name));
  603. dput(dir);
  604. if (!err) {
  605. inode->i_ctime = CURRENT_TIME_SEC;
  606. mark_inode_dirty(inode);
  607. }
  608. out:
  609. return err;
  610. }
  611. /* The following are side effects of other operations that aren't explicitly
  612. * modifying extended attributes. This includes operations such as permissions
  613. * or ownership changes, object deletions, etc. */
  614. static int
  615. reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
  616. loff_t offset, u64 ino, unsigned int d_type)
  617. {
  618. struct dentry *xadir = (struct dentry *)buf;
  619. return __reiserfs_xattr_del(xadir, name, namelen);
  620. }
  621. /* This is called w/ inode->i_mutex downed */
  622. int reiserfs_delete_xattrs(struct inode *inode)
  623. {
  624. struct dentry *dir, *root;
  625. int err = 0;
  626. /* Skip out, an xattr has no xattrs associated with it */
  627. if (is_reiserfs_priv_object(inode) ||
  628. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  629. !reiserfs_xattrs(inode->i_sb)) {
  630. return 0;
  631. }
  632. reiserfs_read_lock_xattrs(inode->i_sb);
  633. dir = open_xa_dir(inode, FL_READONLY);
  634. reiserfs_read_unlock_xattrs(inode->i_sb);
  635. if (IS_ERR(dir)) {
  636. err = PTR_ERR(dir);
  637. goto out;
  638. } else if (!dir->d_inode) {
  639. dput(dir);
  640. return 0;
  641. }
  642. lock_kernel();
  643. err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
  644. if (err) {
  645. unlock_kernel();
  646. goto out_dir;
  647. }
  648. /* Leftovers besides . and .. -- that's not good. */
  649. if (dir->d_inode->i_nlink <= 2) {
  650. root = get_xa_root(inode->i_sb, XATTR_REPLACE);
  651. reiserfs_write_lock_xattrs(inode->i_sb);
  652. err = vfs_rmdir(root->d_inode, dir);
  653. reiserfs_write_unlock_xattrs(inode->i_sb);
  654. dput(root);
  655. } else {
  656. reiserfs_warning(inode->i_sb, "jdm-20006",
  657. "Couldn't remove all entries in directory");
  658. }
  659. unlock_kernel();
  660. out_dir:
  661. dput(dir);
  662. out:
  663. if (!err)
  664. REISERFS_I(inode)->i_flags =
  665. REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
  666. return err;
  667. }
  668. struct reiserfs_chown_buf {
  669. struct inode *inode;
  670. struct dentry *xadir;
  671. struct iattr *attrs;
  672. };
  673. /* XXX: If there is a better way to do this, I'd love to hear about it */
  674. static int
  675. reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
  676. loff_t offset, u64 ino, unsigned int d_type)
  677. {
  678. struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
  679. struct dentry *xafile, *xadir = chown_buf->xadir;
  680. struct iattr *attrs = chown_buf->attrs;
  681. int err = 0;
  682. xafile = lookup_one_len(name, xadir, namelen);
  683. if (IS_ERR(xafile))
  684. return PTR_ERR(xafile);
  685. else if (!xafile->d_inode) {
  686. dput(xafile);
  687. return -ENODATA;
  688. }
  689. if (!S_ISDIR(xafile->d_inode->i_mode))
  690. err = notify_change(xafile, attrs);
  691. dput(xafile);
  692. return err;
  693. }
  694. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  695. {
  696. struct dentry *dir;
  697. int err = 0;
  698. struct reiserfs_chown_buf buf;
  699. unsigned int ia_valid = attrs->ia_valid;
  700. /* Skip out, an xattr has no xattrs associated with it */
  701. if (is_reiserfs_priv_object(inode) ||
  702. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  703. !reiserfs_xattrs(inode->i_sb)) {
  704. return 0;
  705. }
  706. reiserfs_read_lock_xattrs(inode->i_sb);
  707. dir = open_xa_dir(inode, FL_READONLY);
  708. reiserfs_read_unlock_xattrs(inode->i_sb);
  709. if (IS_ERR(dir)) {
  710. if (PTR_ERR(dir) != -ENODATA)
  711. err = PTR_ERR(dir);
  712. goto out;
  713. } else if (!dir->d_inode) {
  714. dput(dir);
  715. goto out;
  716. }
  717. lock_kernel();
  718. attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
  719. buf.xadir = dir;
  720. buf.attrs = attrs;
  721. buf.inode = inode;
  722. err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
  723. if (err) {
  724. unlock_kernel();
  725. goto out_dir;
  726. }
  727. err = notify_change(dir, attrs);
  728. unlock_kernel();
  729. out_dir:
  730. dput(dir);
  731. out:
  732. attrs->ia_valid = ia_valid;
  733. return err;
  734. }
  735. /* Actual operations that are exported to VFS-land */
  736. /*
  737. * Inode operation getxattr()
  738. * Preliminary locking: we down dentry->d_inode->i_mutex
  739. */
  740. ssize_t
  741. reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  742. size_t size)
  743. {
  744. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  745. int err;
  746. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  747. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  748. return -EOPNOTSUPP;
  749. reiserfs_read_lock_xattr_i(dentry->d_inode);
  750. reiserfs_read_lock_xattrs(dentry->d_sb);
  751. err = xah->get(dentry->d_inode, name, buffer, size);
  752. reiserfs_read_unlock_xattrs(dentry->d_sb);
  753. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  754. return err;
  755. }
  756. /*
  757. * Inode operation setxattr()
  758. *
  759. * dentry->d_inode->i_mutex down
  760. */
  761. int
  762. reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  763. size_t size, int flags)
  764. {
  765. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  766. int err;
  767. int lock;
  768. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  769. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  770. return -EOPNOTSUPP;
  771. reiserfs_write_lock_xattr_i(dentry->d_inode);
  772. lock = !has_xattr_dir(dentry->d_inode);
  773. if (lock)
  774. reiserfs_write_lock_xattrs(dentry->d_sb);
  775. else
  776. reiserfs_read_lock_xattrs(dentry->d_sb);
  777. err = xah->set(dentry->d_inode, name, value, size, flags);
  778. if (lock)
  779. reiserfs_write_unlock_xattrs(dentry->d_sb);
  780. else
  781. reiserfs_read_unlock_xattrs(dentry->d_sb);
  782. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  783. return err;
  784. }
  785. /*
  786. * Inode operation removexattr()
  787. *
  788. * dentry->d_inode->i_mutex down
  789. */
  790. int reiserfs_removexattr(struct dentry *dentry, const char *name)
  791. {
  792. int err;
  793. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  794. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  795. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  796. return -EOPNOTSUPP;
  797. reiserfs_write_lock_xattr_i(dentry->d_inode);
  798. reiserfs_read_lock_xattrs(dentry->d_sb);
  799. /* Deletion pre-operation */
  800. if (xah->del) {
  801. err = xah->del(dentry->d_inode, name);
  802. if (err)
  803. goto out;
  804. }
  805. err = reiserfs_xattr_del(dentry->d_inode, name);
  806. dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
  807. mark_inode_dirty(dentry->d_inode);
  808. out:
  809. reiserfs_read_unlock_xattrs(dentry->d_sb);
  810. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  811. return err;
  812. }
  813. /* This is what filldir will use:
  814. * r_pos will always contain the amount of space required for the entire
  815. * list. If r_pos becomes larger than r_size, we need more space and we
  816. * return an error indicating this. If r_pos is less than r_size, then we've
  817. * filled the buffer successfully and we return success */
  818. struct reiserfs_listxattr_buf {
  819. int r_pos;
  820. int r_size;
  821. char *r_buf;
  822. struct inode *r_inode;
  823. };
  824. static int
  825. reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
  826. loff_t offset, u64 ino, unsigned int d_type)
  827. {
  828. struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
  829. int len = 0;
  830. if (name[0] != '.'
  831. || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  832. struct reiserfs_xattr_handler *xah =
  833. find_xattr_handler_prefix(name);
  834. if (!xah)
  835. return 0; /* Unsupported xattr name, skip it */
  836. /* We call ->list() twice because the operation isn't required to just
  837. * return the name back - we want to make sure we have enough space */
  838. len += xah->list(b->r_inode, name, namelen, NULL);
  839. if (len) {
  840. if (b->r_pos + len + 1 <= b->r_size) {
  841. char *p = b->r_buf + b->r_pos;
  842. p += xah->list(b->r_inode, name, namelen, p);
  843. *p++ = '\0';
  844. }
  845. b->r_pos += len + 1;
  846. }
  847. }
  848. return 0;
  849. }
  850. /*
  851. * Inode operation listxattr()
  852. *
  853. * Preliminary locking: we down dentry->d_inode->i_mutex
  854. */
  855. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  856. {
  857. struct dentry *dir;
  858. int err = 0;
  859. struct reiserfs_listxattr_buf buf;
  860. if (!dentry->d_inode)
  861. return -EINVAL;
  862. if (!reiserfs_xattrs(dentry->d_sb) ||
  863. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  864. return -EOPNOTSUPP;
  865. reiserfs_read_lock_xattr_i(dentry->d_inode);
  866. reiserfs_read_lock_xattrs(dentry->d_sb);
  867. dir = open_xa_dir(dentry->d_inode, FL_READONLY);
  868. reiserfs_read_unlock_xattrs(dentry->d_sb);
  869. if (IS_ERR(dir)) {
  870. err = PTR_ERR(dir);
  871. if (err == -ENODATA)
  872. err = 0; /* Not an error if there aren't any xattrs */
  873. goto out;
  874. }
  875. buf.r_buf = buffer;
  876. buf.r_size = buffer ? size : 0;
  877. buf.r_pos = 0;
  878. buf.r_inode = dentry->d_inode;
  879. REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
  880. err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
  881. if (err)
  882. goto out_dir;
  883. if (buf.r_pos > buf.r_size && buffer != NULL)
  884. err = -ERANGE;
  885. else
  886. err = buf.r_pos;
  887. out_dir:
  888. dput(dir);
  889. out:
  890. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  891. return err;
  892. }
  893. /* This is the implementation for the xattr plugin infrastructure */
  894. static LIST_HEAD(xattr_handlers);
  895. static DEFINE_RWLOCK(handler_lock);
  896. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  897. *prefix)
  898. {
  899. struct reiserfs_xattr_handler *xah = NULL;
  900. struct list_head *p;
  901. read_lock(&handler_lock);
  902. list_for_each(p, &xattr_handlers) {
  903. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  904. if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
  905. break;
  906. xah = NULL;
  907. }
  908. read_unlock(&handler_lock);
  909. return xah;
  910. }
  911. static void __unregister_handlers(void)
  912. {
  913. struct reiserfs_xattr_handler *xah;
  914. struct list_head *p, *tmp;
  915. list_for_each_safe(p, tmp, &xattr_handlers) {
  916. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  917. if (xah->exit)
  918. xah->exit();
  919. list_del_init(p);
  920. }
  921. INIT_LIST_HEAD(&xattr_handlers);
  922. }
  923. int __init reiserfs_xattr_register_handlers(void)
  924. {
  925. int err = 0;
  926. struct reiserfs_xattr_handler *xah;
  927. struct list_head *p;
  928. write_lock(&handler_lock);
  929. /* If we're already initialized, nothing to do */
  930. if (!list_empty(&xattr_handlers)) {
  931. write_unlock(&handler_lock);
  932. return 0;
  933. }
  934. /* Add the handlers */
  935. list_add_tail(&user_handler.handlers, &xattr_handlers);
  936. list_add_tail(&trusted_handler.handlers, &xattr_handlers);
  937. #ifdef CONFIG_REISERFS_FS_SECURITY
  938. list_add_tail(&security_handler.handlers, &xattr_handlers);
  939. #endif
  940. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  941. list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
  942. list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
  943. #endif
  944. /* Run initializers, if available */
  945. list_for_each(p, &xattr_handlers) {
  946. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  947. if (xah->init) {
  948. err = xah->init();
  949. if (err) {
  950. list_del_init(p);
  951. break;
  952. }
  953. }
  954. }
  955. /* Clean up other handlers, if any failed */
  956. if (err)
  957. __unregister_handlers();
  958. write_unlock(&handler_lock);
  959. return err;
  960. }
  961. void reiserfs_xattr_unregister_handlers(void)
  962. {
  963. write_lock(&handler_lock);
  964. __unregister_handlers();
  965. write_unlock(&handler_lock);
  966. }
  967. /* This will catch lookups from the fs root to .reiserfs_priv */
  968. static int
  969. xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
  970. {
  971. struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
  972. if (name->len == priv_root->d_name.len &&
  973. name->hash == priv_root->d_name.hash &&
  974. !memcmp(name->name, priv_root->d_name.name, name->len)) {
  975. return -ENOENT;
  976. } else if (q1->len == name->len &&
  977. !memcmp(q1->name, name->name, name->len))
  978. return 0;
  979. return 1;
  980. }
  981. static struct dentry_operations xattr_lookup_poison_ops = {
  982. .d_compare = xattr_lookup_poison,
  983. };
  984. /* We need to take a copy of the mount flags since things like
  985. * MS_RDONLY don't get set until *after* we're called.
  986. * mount_flags != mount_options */
  987. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  988. {
  989. int err = 0;
  990. /* We need generation numbers to ensure that the oid mapping is correct
  991. * v3.5 filesystems don't have them. */
  992. if (!old_format_only(s)) {
  993. set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  994. } else if (reiserfs_xattrs_optional(s)) {
  995. /* Old format filesystem, but optional xattrs have been enabled
  996. * at mount time. Error out. */
  997. reiserfs_warning(s, "jdm-20005",
  998. "xattrs/ACLs not supported on pre v3.6 "
  999. "format filesystem. Failing mount.");
  1000. err = -EOPNOTSUPP;
  1001. goto error;
  1002. } else {
  1003. /* Old format filesystem, but no optional xattrs have been enabled. This
  1004. * means we silently disable xattrs on the filesystem. */
  1005. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1006. }
  1007. /* If we don't have the privroot located yet - go find it */
  1008. if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
  1009. struct dentry *dentry;
  1010. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  1011. strlen(PRIVROOT_NAME));
  1012. if (!IS_ERR(dentry)) {
  1013. if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
  1014. struct inode *inode = dentry->d_parent->d_inode;
  1015. mutex_lock_nested(&inode->i_mutex,
  1016. I_MUTEX_XATTR);
  1017. err = inode->i_op->mkdir(inode, dentry, 0700);
  1018. mutex_unlock(&inode->i_mutex);
  1019. if (err) {
  1020. dput(dentry);
  1021. dentry = NULL;
  1022. }
  1023. if (dentry && dentry->d_inode)
  1024. reiserfs_info(s, "Created %s - "
  1025. "reserved for xattr "
  1026. "storage.\n",
  1027. PRIVROOT_NAME);
  1028. } else if (!dentry->d_inode) {
  1029. dput(dentry);
  1030. dentry = NULL;
  1031. }
  1032. } else
  1033. err = PTR_ERR(dentry);
  1034. if (!err && dentry) {
  1035. s->s_root->d_op = &xattr_lookup_poison_ops;
  1036. reiserfs_mark_inode_private(dentry->d_inode);
  1037. REISERFS_SB(s)->priv_root = dentry;
  1038. } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
  1039. /* If we're read-only it just means that the dir hasn't been
  1040. * created. Not an error -- just no xattrs on the fs. We'll
  1041. * check again if we go read-write */
  1042. reiserfs_warning(s, "jdm-20006",
  1043. "xattrs/ACLs enabled and couldn't "
  1044. "find/create .reiserfs_priv. "
  1045. "Failing mount.");
  1046. err = -EOPNOTSUPP;
  1047. }
  1048. }
  1049. error:
  1050. /* This is only nonzero if there was an error initializing the xattr
  1051. * directory or if there is a condition where we don't support them. */
  1052. if (err) {
  1053. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1054. clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  1055. clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  1056. }
  1057. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  1058. s->s_flags = s->s_flags & ~MS_POSIXACL;
  1059. if (reiserfs_posixacl(s))
  1060. s->s_flags |= MS_POSIXACL;
  1061. return err;
  1062. }
  1063. static int reiserfs_check_acl(struct inode *inode, int mask)
  1064. {
  1065. struct posix_acl *acl;
  1066. int error = -EAGAIN; /* do regular unix permission checks by default */
  1067. reiserfs_read_lock_xattr_i(inode);
  1068. reiserfs_read_lock_xattrs(inode->i_sb);
  1069. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  1070. reiserfs_read_unlock_xattrs(inode->i_sb);
  1071. reiserfs_read_unlock_xattr_i(inode);
  1072. if (acl) {
  1073. if (!IS_ERR(acl)) {
  1074. error = posix_acl_permission(inode, acl, mask);
  1075. posix_acl_release(acl);
  1076. } else if (PTR_ERR(acl) != -ENODATA)
  1077. error = PTR_ERR(acl);
  1078. }
  1079. return error;
  1080. }
  1081. int reiserfs_permission(struct inode *inode, int mask)
  1082. {
  1083. /*
  1084. * We don't do permission checks on the internal objects.
  1085. * Permissions are determined by the "owning" object.
  1086. */
  1087. if (is_reiserfs_priv_object(inode))
  1088. return 0;
  1089. /*
  1090. * Stat data v1 doesn't support ACLs.
  1091. */
  1092. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  1093. return generic_permission(inode, mask, NULL);
  1094. else
  1095. return generic_permission(inode, mask, reiserfs_check_acl);
  1096. }