xattr.c 34 KB

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