xattr.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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, size_t 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 >> PAGE_CACHE_SHIFT, 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. size_t file_pos = 0;
  373. size_t buffer_pos = 0;
  374. struct iattr newattrs;
  375. __u32 xahash = 0;
  376. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  377. return -EOPNOTSUPP;
  378. /* Empty xattrs are ok, they're just empty files, no hash */
  379. if (buffer && buffer_size)
  380. xahash = xattr_hash(buffer, buffer_size);
  381. open_file:
  382. dentry = get_xa_file_dentry(inode, name, flags);
  383. if (IS_ERR(dentry)) {
  384. err = PTR_ERR(dentry);
  385. goto out;
  386. }
  387. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  388. /* we need to copy it off.. */
  389. if (dentry->d_inode->i_nlink > 1) {
  390. dput(dentry);
  391. err = reiserfs_xattr_del(inode, name);
  392. if (err < 0)
  393. goto out;
  394. /* We just killed the old one, we're not replacing anymore */
  395. if (flags & XATTR_REPLACE)
  396. flags &= ~XATTR_REPLACE;
  397. goto open_file;
  398. }
  399. /* Resize it so we're ok to write there */
  400. newattrs.ia_size = buffer_size;
  401. newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
  402. mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
  403. err = notify_change(dentry, &newattrs);
  404. if (err)
  405. goto out_filp;
  406. while (buffer_pos < buffer_size || buffer_pos == 0) {
  407. size_t chunk;
  408. size_t skip = 0;
  409. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  410. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  411. chunk = PAGE_CACHE_SIZE;
  412. else
  413. chunk = buffer_size - buffer_pos;
  414. page = reiserfs_get_page(dentry->d_inode, file_pos);
  415. if (IS_ERR(page)) {
  416. err = PTR_ERR(page);
  417. goto out_filp;
  418. }
  419. lock_page(page);
  420. data = page_address(page);
  421. if (file_pos == 0) {
  422. struct reiserfs_xattr_header *rxh;
  423. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  424. if (chunk + skip > PAGE_CACHE_SIZE)
  425. chunk = PAGE_CACHE_SIZE - skip;
  426. rxh = (struct reiserfs_xattr_header *)data;
  427. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  428. rxh->h_hash = cpu_to_le32(xahash);
  429. }
  430. err = reiserfs_prepare_write(NULL, page, page_offset,
  431. page_offset + chunk + skip);
  432. if (!err) {
  433. if (buffer)
  434. memcpy(data + skip, buffer + buffer_pos, chunk);
  435. err = reiserfs_commit_write(NULL, page, page_offset,
  436. page_offset + chunk +
  437. skip);
  438. }
  439. unlock_page(page);
  440. reiserfs_put_page(page);
  441. buffer_pos += chunk;
  442. file_pos += chunk;
  443. skip = 0;
  444. if (err || buffer_size == 0 || !buffer)
  445. break;
  446. }
  447. /* We can't mark the inode dirty if it's not hashed. This is the case
  448. * when we're inheriting the default ACL. If we dirty it, the inode
  449. * gets marked dirty, but won't (ever) make it onto the dirty list until
  450. * it's synced explicitly to clear I_DIRTY. This is bad. */
  451. if (!hlist_unhashed(&inode->i_hash)) {
  452. inode->i_ctime = CURRENT_TIME_SEC;
  453. mark_inode_dirty(inode);
  454. }
  455. out_filp:
  456. mutex_unlock(&dentry->d_inode->i_mutex);
  457. dput(dentry);
  458. out:
  459. return err;
  460. }
  461. /*
  462. * inode->i_mutex: down
  463. */
  464. int
  465. reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
  466. size_t buffer_size)
  467. {
  468. ssize_t err = 0;
  469. struct dentry *dentry;
  470. size_t isize;
  471. size_t file_pos = 0;
  472. size_t buffer_pos = 0;
  473. struct page *page;
  474. __u32 hash = 0;
  475. if (name == NULL)
  476. return -EINVAL;
  477. /* We can't have xattrs attached to v1 items since they don't have
  478. * generation numbers */
  479. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  480. return -EOPNOTSUPP;
  481. dentry = get_xa_file_dentry(inode, name, FL_READONLY);
  482. if (IS_ERR(dentry)) {
  483. err = PTR_ERR(dentry);
  484. goto out;
  485. }
  486. isize = i_size_read(dentry->d_inode);
  487. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  488. /* Just return the size needed */
  489. if (buffer == NULL) {
  490. err = isize - sizeof(struct reiserfs_xattr_header);
  491. goto out_dput;
  492. }
  493. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  494. err = -ERANGE;
  495. goto out_dput;
  496. }
  497. while (file_pos < isize) {
  498. size_t chunk;
  499. char *data;
  500. size_t skip = 0;
  501. if (isize - file_pos > PAGE_CACHE_SIZE)
  502. chunk = PAGE_CACHE_SIZE;
  503. else
  504. chunk = isize - file_pos;
  505. page = reiserfs_get_page(dentry->d_inode, file_pos);
  506. if (IS_ERR(page)) {
  507. err = PTR_ERR(page);
  508. goto out_dput;
  509. }
  510. lock_page(page);
  511. data = page_address(page);
  512. if (file_pos == 0) {
  513. struct reiserfs_xattr_header *rxh =
  514. (struct reiserfs_xattr_header *)data;
  515. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  516. chunk -= skip;
  517. /* Magic doesn't match up.. */
  518. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  519. unlock_page(page);
  520. reiserfs_put_page(page);
  521. reiserfs_warning(inode->i_sb, "jdm-20001",
  522. "Invalid magic for xattr (%s) "
  523. "associated with %k", name,
  524. INODE_PKEY(inode));
  525. err = -EIO;
  526. goto out_dput;
  527. }
  528. hash = le32_to_cpu(rxh->h_hash);
  529. }
  530. memcpy(buffer + buffer_pos, data + skip, chunk);
  531. unlock_page(page);
  532. reiserfs_put_page(page);
  533. file_pos += chunk;
  534. buffer_pos += chunk;
  535. skip = 0;
  536. }
  537. err = isize - sizeof(struct reiserfs_xattr_header);
  538. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  539. hash) {
  540. reiserfs_warning(inode->i_sb, "jdm-20002",
  541. "Invalid hash for xattr (%s) associated "
  542. "with %k", name, INODE_PKEY(inode));
  543. err = -EIO;
  544. }
  545. out_dput:
  546. dput(dentry);
  547. out:
  548. return err;
  549. }
  550. static int
  551. __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
  552. {
  553. struct dentry *dentry;
  554. struct inode *dir = xadir->d_inode;
  555. int err = 0;
  556. dentry = lookup_one_len(name, xadir, namelen);
  557. if (IS_ERR(dentry)) {
  558. err = PTR_ERR(dentry);
  559. goto out;
  560. } else if (!dentry->d_inode) {
  561. err = -ENODATA;
  562. goto out_file;
  563. }
  564. /* Skip directories.. */
  565. if (S_ISDIR(dentry->d_inode->i_mode))
  566. goto out_file;
  567. if (!is_reiserfs_priv_object(dentry->d_inode)) {
  568. reiserfs_error(dir->i_sb, "jdm-20003",
  569. "OID %08x [%.*s/%.*s] doesn't have "
  570. "priv flag set [parent is %sset].",
  571. le32_to_cpu(INODE_PKEY(dentry->d_inode)->
  572. k_objectid), xadir->d_name.len,
  573. xadir->d_name.name, namelen, name,
  574. is_reiserfs_priv_object(xadir->d_inode) ? "" :
  575. "not ");
  576. dput(dentry);
  577. return -EIO;
  578. }
  579. err = dir->i_op->unlink(dir, dentry);
  580. if (!err)
  581. d_delete(dentry);
  582. out_file:
  583. dput(dentry);
  584. out:
  585. return err;
  586. }
  587. int reiserfs_xattr_del(struct inode *inode, const char *name)
  588. {
  589. struct dentry *dir;
  590. int err;
  591. dir = open_xa_dir(inode, FL_READONLY);
  592. if (IS_ERR(dir)) {
  593. err = PTR_ERR(dir);
  594. goto out;
  595. }
  596. err = __reiserfs_xattr_del(dir, name, strlen(name));
  597. dput(dir);
  598. if (!err) {
  599. inode->i_ctime = CURRENT_TIME_SEC;
  600. mark_inode_dirty(inode);
  601. }
  602. out:
  603. return err;
  604. }
  605. /* The following are side effects of other operations that aren't explicitly
  606. * modifying extended attributes. This includes operations such as permissions
  607. * or ownership changes, object deletions, etc. */
  608. static int
  609. reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
  610. loff_t offset, u64 ino, unsigned int d_type)
  611. {
  612. struct dentry *xadir = (struct dentry *)buf;
  613. return __reiserfs_xattr_del(xadir, name, namelen);
  614. }
  615. /* This is called w/ inode->i_mutex downed */
  616. int reiserfs_delete_xattrs(struct inode *inode)
  617. {
  618. struct dentry *dir, *root;
  619. int err = 0;
  620. /* Skip out, an xattr has no xattrs associated with it */
  621. if (is_reiserfs_priv_object(inode) ||
  622. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  623. !reiserfs_xattrs(inode->i_sb)) {
  624. return 0;
  625. }
  626. reiserfs_read_lock_xattrs(inode->i_sb);
  627. dir = open_xa_dir(inode, FL_READONLY);
  628. reiserfs_read_unlock_xattrs(inode->i_sb);
  629. if (IS_ERR(dir)) {
  630. err = PTR_ERR(dir);
  631. goto out;
  632. } else if (!dir->d_inode) {
  633. dput(dir);
  634. return 0;
  635. }
  636. lock_kernel();
  637. err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
  638. if (err) {
  639. unlock_kernel();
  640. goto out_dir;
  641. }
  642. /* Leftovers besides . and .. -- that's not good. */
  643. if (dir->d_inode->i_nlink <= 2) {
  644. root = get_xa_root(inode->i_sb, XATTR_REPLACE);
  645. reiserfs_write_lock_xattrs(inode->i_sb);
  646. err = vfs_rmdir(root->d_inode, dir);
  647. reiserfs_write_unlock_xattrs(inode->i_sb);
  648. dput(root);
  649. } else {
  650. reiserfs_warning(inode->i_sb, "jdm-20006",
  651. "Couldn't remove all entries in directory");
  652. }
  653. unlock_kernel();
  654. out_dir:
  655. dput(dir);
  656. out:
  657. if (!err)
  658. REISERFS_I(inode)->i_flags =
  659. REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
  660. return err;
  661. }
  662. struct reiserfs_chown_buf {
  663. struct inode *inode;
  664. struct dentry *xadir;
  665. struct iattr *attrs;
  666. };
  667. /* XXX: If there is a better way to do this, I'd love to hear about it */
  668. static int
  669. reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
  670. loff_t offset, u64 ino, unsigned int d_type)
  671. {
  672. struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
  673. struct dentry *xafile, *xadir = chown_buf->xadir;
  674. struct iattr *attrs = chown_buf->attrs;
  675. int err = 0;
  676. xafile = lookup_one_len(name, xadir, namelen);
  677. if (IS_ERR(xafile))
  678. return PTR_ERR(xafile);
  679. else if (!xafile->d_inode) {
  680. dput(xafile);
  681. return -ENODATA;
  682. }
  683. if (!S_ISDIR(xafile->d_inode->i_mode))
  684. err = notify_change(xafile, attrs);
  685. dput(xafile);
  686. return err;
  687. }
  688. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  689. {
  690. struct dentry *dir;
  691. int err = 0;
  692. struct reiserfs_chown_buf buf;
  693. unsigned int ia_valid = attrs->ia_valid;
  694. /* Skip out, an xattr has no xattrs associated with it */
  695. if (is_reiserfs_priv_object(inode) ||
  696. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  697. !reiserfs_xattrs(inode->i_sb)) {
  698. return 0;
  699. }
  700. reiserfs_read_lock_xattrs(inode->i_sb);
  701. dir = open_xa_dir(inode, FL_READONLY);
  702. reiserfs_read_unlock_xattrs(inode->i_sb);
  703. if (IS_ERR(dir)) {
  704. if (PTR_ERR(dir) != -ENODATA)
  705. err = PTR_ERR(dir);
  706. goto out;
  707. } else if (!dir->d_inode) {
  708. dput(dir);
  709. goto out;
  710. }
  711. lock_kernel();
  712. attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
  713. buf.xadir = dir;
  714. buf.attrs = attrs;
  715. buf.inode = inode;
  716. err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
  717. if (err) {
  718. unlock_kernel();
  719. goto out_dir;
  720. }
  721. err = notify_change(dir, attrs);
  722. unlock_kernel();
  723. out_dir:
  724. dput(dir);
  725. out:
  726. attrs->ia_valid = ia_valid;
  727. return err;
  728. }
  729. /* Actual operations that are exported to VFS-land */
  730. /*
  731. * Inode operation getxattr()
  732. * Preliminary locking: we down dentry->d_inode->i_mutex
  733. */
  734. ssize_t
  735. reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  736. size_t size)
  737. {
  738. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  739. int err;
  740. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  741. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  742. return -EOPNOTSUPP;
  743. reiserfs_read_lock_xattr_i(dentry->d_inode);
  744. reiserfs_read_lock_xattrs(dentry->d_sb);
  745. err = xah->get(dentry->d_inode, name, buffer, size);
  746. reiserfs_read_unlock_xattrs(dentry->d_sb);
  747. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  748. return err;
  749. }
  750. /*
  751. * Inode operation setxattr()
  752. *
  753. * dentry->d_inode->i_mutex down
  754. */
  755. int
  756. reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  757. size_t size, int flags)
  758. {
  759. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  760. int err;
  761. int lock;
  762. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  763. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  764. return -EOPNOTSUPP;
  765. reiserfs_write_lock_xattr_i(dentry->d_inode);
  766. lock = !has_xattr_dir(dentry->d_inode);
  767. if (lock)
  768. reiserfs_write_lock_xattrs(dentry->d_sb);
  769. else
  770. reiserfs_read_lock_xattrs(dentry->d_sb);
  771. err = xah->set(dentry->d_inode, name, value, size, flags);
  772. if (lock)
  773. reiserfs_write_unlock_xattrs(dentry->d_sb);
  774. else
  775. reiserfs_read_unlock_xattrs(dentry->d_sb);
  776. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  777. return err;
  778. }
  779. /*
  780. * Inode operation removexattr()
  781. *
  782. * dentry->d_inode->i_mutex down
  783. */
  784. int reiserfs_removexattr(struct dentry *dentry, const char *name)
  785. {
  786. int err;
  787. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  788. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  789. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  790. return -EOPNOTSUPP;
  791. reiserfs_write_lock_xattr_i(dentry->d_inode);
  792. reiserfs_read_lock_xattrs(dentry->d_sb);
  793. /* Deletion pre-operation */
  794. if (xah->del) {
  795. err = xah->del(dentry->d_inode, name);
  796. if (err)
  797. goto out;
  798. }
  799. err = reiserfs_xattr_del(dentry->d_inode, name);
  800. dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
  801. mark_inode_dirty(dentry->d_inode);
  802. out:
  803. reiserfs_read_unlock_xattrs(dentry->d_sb);
  804. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  805. return err;
  806. }
  807. /* This is what filldir will use:
  808. * r_pos will always contain the amount of space required for the entire
  809. * list. If r_pos becomes larger than r_size, we need more space and we
  810. * return an error indicating this. If r_pos is less than r_size, then we've
  811. * filled the buffer successfully and we return success */
  812. struct reiserfs_listxattr_buf {
  813. int r_pos;
  814. int r_size;
  815. char *r_buf;
  816. struct inode *r_inode;
  817. };
  818. static int
  819. reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
  820. loff_t offset, u64 ino, unsigned int d_type)
  821. {
  822. struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
  823. int len = 0;
  824. if (name[0] != '.'
  825. || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  826. struct reiserfs_xattr_handler *xah =
  827. find_xattr_handler_prefix(name);
  828. if (!xah)
  829. return 0; /* Unsupported xattr name, skip it */
  830. /* We call ->list() twice because the operation isn't required to just
  831. * return the name back - we want to make sure we have enough space */
  832. len += xah->list(b->r_inode, name, namelen, NULL);
  833. if (len) {
  834. if (b->r_pos + len + 1 <= b->r_size) {
  835. char *p = b->r_buf + b->r_pos;
  836. p += xah->list(b->r_inode, name, namelen, p);
  837. *p++ = '\0';
  838. }
  839. b->r_pos += len + 1;
  840. }
  841. }
  842. return 0;
  843. }
  844. /*
  845. * Inode operation listxattr()
  846. *
  847. * Preliminary locking: we down dentry->d_inode->i_mutex
  848. */
  849. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  850. {
  851. struct dentry *dir;
  852. int err = 0;
  853. struct reiserfs_listxattr_buf buf;
  854. if (!dentry->d_inode)
  855. return -EINVAL;
  856. if (!reiserfs_xattrs(dentry->d_sb) ||
  857. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  858. return -EOPNOTSUPP;
  859. reiserfs_read_lock_xattr_i(dentry->d_inode);
  860. reiserfs_read_lock_xattrs(dentry->d_sb);
  861. dir = open_xa_dir(dentry->d_inode, FL_READONLY);
  862. reiserfs_read_unlock_xattrs(dentry->d_sb);
  863. if (IS_ERR(dir)) {
  864. err = PTR_ERR(dir);
  865. if (err == -ENODATA)
  866. err = 0; /* Not an error if there aren't any xattrs */
  867. goto out;
  868. }
  869. buf.r_buf = buffer;
  870. buf.r_size = buffer ? size : 0;
  871. buf.r_pos = 0;
  872. buf.r_inode = dentry->d_inode;
  873. REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
  874. err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
  875. if (err)
  876. goto out_dir;
  877. if (buf.r_pos > buf.r_size && buffer != NULL)
  878. err = -ERANGE;
  879. else
  880. err = buf.r_pos;
  881. out_dir:
  882. dput(dir);
  883. out:
  884. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  885. return err;
  886. }
  887. /* This is the implementation for the xattr plugin infrastructure */
  888. static LIST_HEAD(xattr_handlers);
  889. static DEFINE_RWLOCK(handler_lock);
  890. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  891. *prefix)
  892. {
  893. struct reiserfs_xattr_handler *xah = NULL;
  894. struct list_head *p;
  895. read_lock(&handler_lock);
  896. list_for_each(p, &xattr_handlers) {
  897. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  898. if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
  899. break;
  900. xah = NULL;
  901. }
  902. read_unlock(&handler_lock);
  903. return xah;
  904. }
  905. static void __unregister_handlers(void)
  906. {
  907. struct reiserfs_xattr_handler *xah;
  908. struct list_head *p, *tmp;
  909. list_for_each_safe(p, tmp, &xattr_handlers) {
  910. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  911. if (xah->exit)
  912. xah->exit();
  913. list_del_init(p);
  914. }
  915. INIT_LIST_HEAD(&xattr_handlers);
  916. }
  917. int __init reiserfs_xattr_register_handlers(void)
  918. {
  919. int err = 0;
  920. struct reiserfs_xattr_handler *xah;
  921. struct list_head *p;
  922. write_lock(&handler_lock);
  923. /* If we're already initialized, nothing to do */
  924. if (!list_empty(&xattr_handlers)) {
  925. write_unlock(&handler_lock);
  926. return 0;
  927. }
  928. /* Add the handlers */
  929. list_add_tail(&user_handler.handlers, &xattr_handlers);
  930. list_add_tail(&trusted_handler.handlers, &xattr_handlers);
  931. #ifdef CONFIG_REISERFS_FS_SECURITY
  932. list_add_tail(&security_handler.handlers, &xattr_handlers);
  933. #endif
  934. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  935. list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
  936. list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
  937. #endif
  938. /* Run initializers, if available */
  939. list_for_each(p, &xattr_handlers) {
  940. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  941. if (xah->init) {
  942. err = xah->init();
  943. if (err) {
  944. list_del_init(p);
  945. break;
  946. }
  947. }
  948. }
  949. /* Clean up other handlers, if any failed */
  950. if (err)
  951. __unregister_handlers();
  952. write_unlock(&handler_lock);
  953. return err;
  954. }
  955. void reiserfs_xattr_unregister_handlers(void)
  956. {
  957. write_lock(&handler_lock);
  958. __unregister_handlers();
  959. write_unlock(&handler_lock);
  960. }
  961. /* This will catch lookups from the fs root to .reiserfs_priv */
  962. static int
  963. xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
  964. {
  965. struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
  966. if (name->len == priv_root->d_name.len &&
  967. name->hash == priv_root->d_name.hash &&
  968. !memcmp(name->name, priv_root->d_name.name, name->len)) {
  969. return -ENOENT;
  970. } else if (q1->len == name->len &&
  971. !memcmp(q1->name, name->name, name->len))
  972. return 0;
  973. return 1;
  974. }
  975. static struct dentry_operations xattr_lookup_poison_ops = {
  976. .d_compare = xattr_lookup_poison,
  977. };
  978. /* We need to take a copy of the mount flags since things like
  979. * MS_RDONLY don't get set until *after* we're called.
  980. * mount_flags != mount_options */
  981. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  982. {
  983. int err = 0;
  984. /* We need generation numbers to ensure that the oid mapping is correct
  985. * v3.5 filesystems don't have them. */
  986. if (!old_format_only(s)) {
  987. set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  988. } else if (reiserfs_xattrs_optional(s)) {
  989. /* Old format filesystem, but optional xattrs have been enabled
  990. * at mount time. Error out. */
  991. reiserfs_warning(s, "jdm-20005",
  992. "xattrs/ACLs not supported on pre v3.6 "
  993. "format filesystem. Failing mount.");
  994. err = -EOPNOTSUPP;
  995. goto error;
  996. } else {
  997. /* Old format filesystem, but no optional xattrs have been enabled. This
  998. * means we silently disable xattrs on the filesystem. */
  999. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1000. }
  1001. /* If we don't have the privroot located yet - go find it */
  1002. if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
  1003. struct dentry *dentry;
  1004. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  1005. strlen(PRIVROOT_NAME));
  1006. if (!IS_ERR(dentry)) {
  1007. if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
  1008. struct inode *inode = dentry->d_parent->d_inode;
  1009. mutex_lock_nested(&inode->i_mutex,
  1010. I_MUTEX_XATTR);
  1011. err = inode->i_op->mkdir(inode, dentry, 0700);
  1012. mutex_unlock(&inode->i_mutex);
  1013. if (err) {
  1014. dput(dentry);
  1015. dentry = NULL;
  1016. }
  1017. if (dentry && dentry->d_inode)
  1018. reiserfs_info(s, "Created %s - "
  1019. "reserved for xattr "
  1020. "storage.\n",
  1021. PRIVROOT_NAME);
  1022. } else if (!dentry->d_inode) {
  1023. dput(dentry);
  1024. dentry = NULL;
  1025. }
  1026. } else
  1027. err = PTR_ERR(dentry);
  1028. if (!err && dentry) {
  1029. s->s_root->d_op = &xattr_lookup_poison_ops;
  1030. reiserfs_mark_inode_private(dentry->d_inode);
  1031. REISERFS_SB(s)->priv_root = dentry;
  1032. } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
  1033. /* If we're read-only it just means that the dir hasn't been
  1034. * created. Not an error -- just no xattrs on the fs. We'll
  1035. * check again if we go read-write */
  1036. reiserfs_warning(s, "jdm-20006",
  1037. "xattrs/ACLs enabled and couldn't "
  1038. "find/create .reiserfs_priv. "
  1039. "Failing mount.");
  1040. err = -EOPNOTSUPP;
  1041. }
  1042. }
  1043. error:
  1044. /* This is only nonzero if there was an error initializing the xattr
  1045. * directory or if there is a condition where we don't support them. */
  1046. if (err) {
  1047. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1048. clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  1049. clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  1050. }
  1051. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  1052. s->s_flags = s->s_flags & ~MS_POSIXACL;
  1053. if (reiserfs_posixacl(s))
  1054. s->s_flags |= MS_POSIXACL;
  1055. return err;
  1056. }
  1057. static int reiserfs_check_acl(struct inode *inode, int mask)
  1058. {
  1059. struct posix_acl *acl;
  1060. int error = -EAGAIN; /* do regular unix permission checks by default */
  1061. reiserfs_read_lock_xattr_i(inode);
  1062. reiserfs_read_lock_xattrs(inode->i_sb);
  1063. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  1064. reiserfs_read_unlock_xattrs(inode->i_sb);
  1065. reiserfs_read_unlock_xattr_i(inode);
  1066. if (acl) {
  1067. if (!IS_ERR(acl)) {
  1068. error = posix_acl_permission(inode, acl, mask);
  1069. posix_acl_release(acl);
  1070. } else if (PTR_ERR(acl) != -ENODATA)
  1071. error = PTR_ERR(acl);
  1072. }
  1073. return error;
  1074. }
  1075. int reiserfs_permission(struct inode *inode, int mask)
  1076. {
  1077. /*
  1078. * We don't do permission checks on the internal objects.
  1079. * Permissions are determined by the "owning" object.
  1080. */
  1081. if (is_reiserfs_priv_object(inode))
  1082. return 0;
  1083. /*
  1084. * Stat data v1 doesn't support ACLs.
  1085. */
  1086. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  1087. return generic_permission(inode, mask, NULL);
  1088. else
  1089. return generic_permission(inode, mask, reiserfs_check_acl);
  1090. }