ops_inode.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/utsname.h>
  15. #include <linux/mm.h>
  16. #include <linux/xattr.h>
  17. #include <linux/posix_acl.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/crc32.h>
  20. #include <linux/fiemap.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "eaops.h"
  28. #include "eattr.h"
  29. #include "glock.h"
  30. #include "inode.h"
  31. #include "meta_io.h"
  32. #include "quota.h"
  33. #include "rgrp.h"
  34. #include "trans.h"
  35. #include "util.h"
  36. #include "super.h"
  37. /**
  38. * gfs2_create - Create a file
  39. * @dir: The directory in which to create the file
  40. * @dentry: The dentry of the new file
  41. * @mode: The mode of the new file
  42. *
  43. * Returns: errno
  44. */
  45. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  46. int mode, struct nameidata *nd)
  47. {
  48. struct gfs2_inode *dip = GFS2_I(dir);
  49. struct gfs2_sbd *sdp = GFS2_SB(dir);
  50. struct gfs2_holder ghs[2];
  51. struct inode *inode;
  52. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  53. for (;;) {
  54. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
  55. if (!IS_ERR(inode)) {
  56. gfs2_trans_end(sdp);
  57. if (dip->i_alloc->al_rgd)
  58. gfs2_inplace_release(dip);
  59. gfs2_quota_unlock(dip);
  60. gfs2_alloc_put(dip);
  61. gfs2_glock_dq_uninit_m(2, ghs);
  62. mark_inode_dirty(inode);
  63. break;
  64. } else if (PTR_ERR(inode) != -EEXIST ||
  65. (nd && nd->flags & LOOKUP_EXCL)) {
  66. gfs2_holder_uninit(ghs);
  67. return PTR_ERR(inode);
  68. }
  69. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  70. if (inode) {
  71. if (!IS_ERR(inode)) {
  72. gfs2_holder_uninit(ghs);
  73. break;
  74. } else {
  75. gfs2_holder_uninit(ghs);
  76. return PTR_ERR(inode);
  77. }
  78. }
  79. }
  80. d_instantiate(dentry, inode);
  81. return 0;
  82. }
  83. /**
  84. * gfs2_lookup - Look up a filename in a directory and return its inode
  85. * @dir: The directory inode
  86. * @dentry: The dentry of the new inode
  87. * @nd: passed from Linux VFS, ignored by us
  88. *
  89. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  90. *
  91. * Returns: errno
  92. */
  93. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  94. struct nameidata *nd)
  95. {
  96. struct inode *inode = NULL;
  97. dentry->d_op = &gfs2_dops;
  98. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  99. if (inode && IS_ERR(inode))
  100. return ERR_CAST(inode);
  101. if (inode) {
  102. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  103. struct gfs2_holder gh;
  104. int error;
  105. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  106. if (error) {
  107. iput(inode);
  108. return ERR_PTR(error);
  109. }
  110. gfs2_glock_dq_uninit(&gh);
  111. return d_splice_alias(inode, dentry);
  112. }
  113. d_add(dentry, inode);
  114. return NULL;
  115. }
  116. /**
  117. * gfs2_link - Link to a file
  118. * @old_dentry: The inode to link
  119. * @dir: Add link to this directory
  120. * @dentry: The name of the link
  121. *
  122. * Link the inode in "old_dentry" into the directory "dir" with the
  123. * name in "dentry".
  124. *
  125. * Returns: errno
  126. */
  127. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  128. struct dentry *dentry)
  129. {
  130. struct gfs2_inode *dip = GFS2_I(dir);
  131. struct gfs2_sbd *sdp = GFS2_SB(dir);
  132. struct inode *inode = old_dentry->d_inode;
  133. struct gfs2_inode *ip = GFS2_I(inode);
  134. struct gfs2_holder ghs[2];
  135. int alloc_required;
  136. int error;
  137. if (S_ISDIR(inode->i_mode))
  138. return -EPERM;
  139. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  140. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  141. error = gfs2_glock_nq(ghs); /* parent */
  142. if (error)
  143. goto out_parent;
  144. error = gfs2_glock_nq(ghs + 1); /* child */
  145. if (error)
  146. goto out_child;
  147. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  148. if (error)
  149. goto out_gunlock;
  150. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  151. switch (error) {
  152. case -ENOENT:
  153. break;
  154. case 0:
  155. error = -EEXIST;
  156. default:
  157. goto out_gunlock;
  158. }
  159. error = -EINVAL;
  160. if (!dip->i_inode.i_nlink)
  161. goto out_gunlock;
  162. error = -EFBIG;
  163. if (dip->i_entries == (u32)-1)
  164. goto out_gunlock;
  165. error = -EPERM;
  166. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  167. goto out_gunlock;
  168. error = -EINVAL;
  169. if (!ip->i_inode.i_nlink)
  170. goto out_gunlock;
  171. error = -EMLINK;
  172. if (ip->i_inode.i_nlink == (u32)-1)
  173. goto out_gunlock;
  174. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  175. if (error < 0)
  176. goto out_gunlock;
  177. error = 0;
  178. if (alloc_required) {
  179. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  180. if (!al) {
  181. error = -ENOMEM;
  182. goto out_gunlock;
  183. }
  184. error = gfs2_quota_lock_check(dip);
  185. if (error)
  186. goto out_alloc;
  187. al->al_requested = sdp->sd_max_dirres;
  188. error = gfs2_inplace_reserve(dip);
  189. if (error)
  190. goto out_gunlock_q;
  191. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  192. al->al_rgd->rd_length +
  193. 2 * RES_DINODE + RES_STATFS +
  194. RES_QUOTA, 0);
  195. if (error)
  196. goto out_ipres;
  197. } else {
  198. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  199. if (error)
  200. goto out_ipres;
  201. }
  202. error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
  203. if (error)
  204. goto out_end_trans;
  205. error = gfs2_change_nlink(ip, +1);
  206. out_end_trans:
  207. gfs2_trans_end(sdp);
  208. out_ipres:
  209. if (alloc_required)
  210. gfs2_inplace_release(dip);
  211. out_gunlock_q:
  212. if (alloc_required)
  213. gfs2_quota_unlock(dip);
  214. out_alloc:
  215. if (alloc_required)
  216. gfs2_alloc_put(dip);
  217. out_gunlock:
  218. gfs2_glock_dq(ghs + 1);
  219. out_child:
  220. gfs2_glock_dq(ghs);
  221. out_parent:
  222. gfs2_holder_uninit(ghs);
  223. gfs2_holder_uninit(ghs + 1);
  224. if (!error) {
  225. atomic_inc(&inode->i_count);
  226. d_instantiate(dentry, inode);
  227. mark_inode_dirty(inode);
  228. }
  229. return error;
  230. }
  231. /*
  232. * gfs2_unlink_ok - check to see that a inode is still in a directory
  233. * @dip: the directory
  234. * @name: the name of the file
  235. * @ip: the inode
  236. *
  237. * Assumes that the lock on (at least) @dip is held.
  238. *
  239. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  240. */
  241. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  242. const struct gfs2_inode *ip)
  243. {
  244. int error;
  245. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  246. return -EPERM;
  247. if ((dip->i_inode.i_mode & S_ISVTX) &&
  248. dip->i_inode.i_uid != current_fsuid() &&
  249. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  250. return -EPERM;
  251. if (IS_APPEND(&dip->i_inode))
  252. return -EPERM;
  253. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  254. if (error)
  255. return error;
  256. error = gfs2_dir_check(&dip->i_inode, name, ip);
  257. if (error)
  258. return error;
  259. return 0;
  260. }
  261. /**
  262. * gfs2_unlink - Unlink a file
  263. * @dir: The inode of the directory containing the file to unlink
  264. * @dentry: The file itself
  265. *
  266. * Unlink a file. Call gfs2_unlinki()
  267. *
  268. * Returns: errno
  269. */
  270. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  271. {
  272. struct gfs2_inode *dip = GFS2_I(dir);
  273. struct gfs2_sbd *sdp = GFS2_SB(dir);
  274. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  275. struct gfs2_holder ghs[3];
  276. struct gfs2_rgrpd *rgd;
  277. struct gfs2_holder ri_gh;
  278. int error;
  279. error = gfs2_rindex_hold(sdp, &ri_gh);
  280. if (error)
  281. return error;
  282. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  283. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  284. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  285. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  286. error = gfs2_glock_nq(ghs); /* parent */
  287. if (error)
  288. goto out_parent;
  289. error = gfs2_glock_nq(ghs + 1); /* child */
  290. if (error)
  291. goto out_child;
  292. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  293. if (error)
  294. goto out_rgrp;
  295. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  296. if (error)
  297. goto out_gunlock;
  298. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  299. if (error)
  300. goto out_rgrp;
  301. error = gfs2_dir_del(dip, &dentry->d_name);
  302. if (error)
  303. goto out_end_trans;
  304. error = gfs2_change_nlink(ip, -1);
  305. out_end_trans:
  306. gfs2_trans_end(sdp);
  307. out_gunlock:
  308. gfs2_glock_dq(ghs + 2);
  309. out_rgrp:
  310. gfs2_holder_uninit(ghs + 2);
  311. gfs2_glock_dq(ghs + 1);
  312. out_child:
  313. gfs2_holder_uninit(ghs + 1);
  314. gfs2_glock_dq(ghs);
  315. out_parent:
  316. gfs2_holder_uninit(ghs);
  317. gfs2_glock_dq_uninit(&ri_gh);
  318. return error;
  319. }
  320. /**
  321. * gfs2_symlink - Create a symlink
  322. * @dir: The directory to create the symlink in
  323. * @dentry: The dentry to put the symlink in
  324. * @symname: The thing which the link points to
  325. *
  326. * Returns: errno
  327. */
  328. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  329. const char *symname)
  330. {
  331. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  332. struct gfs2_sbd *sdp = GFS2_SB(dir);
  333. struct gfs2_holder ghs[2];
  334. struct inode *inode;
  335. struct buffer_head *dibh;
  336. int size;
  337. int error;
  338. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  339. size = strlen(symname);
  340. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  341. return -ENAMETOOLONG;
  342. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  343. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  344. if (IS_ERR(inode)) {
  345. gfs2_holder_uninit(ghs);
  346. return PTR_ERR(inode);
  347. }
  348. ip = ghs[1].gh_gl->gl_object;
  349. ip->i_disksize = size;
  350. i_size_write(inode, size);
  351. error = gfs2_meta_inode_buffer(ip, &dibh);
  352. if (!gfs2_assert_withdraw(sdp, !error)) {
  353. gfs2_dinode_out(ip, dibh->b_data);
  354. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  355. size);
  356. brelse(dibh);
  357. }
  358. gfs2_trans_end(sdp);
  359. if (dip->i_alloc->al_rgd)
  360. gfs2_inplace_release(dip);
  361. gfs2_quota_unlock(dip);
  362. gfs2_alloc_put(dip);
  363. gfs2_glock_dq_uninit_m(2, ghs);
  364. d_instantiate(dentry, inode);
  365. mark_inode_dirty(inode);
  366. return 0;
  367. }
  368. /**
  369. * gfs2_mkdir - Make a directory
  370. * @dir: The parent directory of the new one
  371. * @dentry: The dentry of the new directory
  372. * @mode: The mode of the new directory
  373. *
  374. * Returns: errno
  375. */
  376. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  377. {
  378. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  379. struct gfs2_sbd *sdp = GFS2_SB(dir);
  380. struct gfs2_holder ghs[2];
  381. struct inode *inode;
  382. struct buffer_head *dibh;
  383. int error;
  384. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  385. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  386. if (IS_ERR(inode)) {
  387. gfs2_holder_uninit(ghs);
  388. return PTR_ERR(inode);
  389. }
  390. ip = ghs[1].gh_gl->gl_object;
  391. ip->i_inode.i_nlink = 2;
  392. ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  393. ip->i_diskflags |= GFS2_DIF_JDATA;
  394. ip->i_entries = 2;
  395. error = gfs2_meta_inode_buffer(ip, &dibh);
  396. if (!gfs2_assert_withdraw(sdp, !error)) {
  397. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  398. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  399. struct qstr str;
  400. gfs2_str2qstr(&str, ".");
  401. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  402. gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
  403. dent->de_inum = di->di_num; /* already GFS2 endian */
  404. dent->de_type = cpu_to_be16(DT_DIR);
  405. di->di_entries = cpu_to_be32(1);
  406. gfs2_str2qstr(&str, "..");
  407. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  408. gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  409. gfs2_inum_out(dip, dent);
  410. dent->de_type = cpu_to_be16(DT_DIR);
  411. gfs2_dinode_out(ip, di);
  412. brelse(dibh);
  413. }
  414. error = gfs2_change_nlink(dip, +1);
  415. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  416. gfs2_trans_end(sdp);
  417. if (dip->i_alloc->al_rgd)
  418. gfs2_inplace_release(dip);
  419. gfs2_quota_unlock(dip);
  420. gfs2_alloc_put(dip);
  421. gfs2_glock_dq_uninit_m(2, ghs);
  422. d_instantiate(dentry, inode);
  423. mark_inode_dirty(inode);
  424. return 0;
  425. }
  426. /**
  427. * gfs2_rmdiri - Remove a directory
  428. * @dip: The parent directory of the directory to be removed
  429. * @name: The name of the directory to be removed
  430. * @ip: The GFS2 inode of the directory to be removed
  431. *
  432. * Assumes Glocks on dip and ip are held
  433. *
  434. * Returns: errno
  435. */
  436. static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
  437. struct gfs2_inode *ip)
  438. {
  439. struct qstr dotname;
  440. int error;
  441. if (ip->i_entries != 2) {
  442. if (gfs2_consist_inode(ip))
  443. gfs2_dinode_print(ip);
  444. return -EIO;
  445. }
  446. error = gfs2_dir_del(dip, name);
  447. if (error)
  448. return error;
  449. error = gfs2_change_nlink(dip, -1);
  450. if (error)
  451. return error;
  452. gfs2_str2qstr(&dotname, ".");
  453. error = gfs2_dir_del(ip, &dotname);
  454. if (error)
  455. return error;
  456. gfs2_str2qstr(&dotname, "..");
  457. error = gfs2_dir_del(ip, &dotname);
  458. if (error)
  459. return error;
  460. /* It looks odd, but it really should be done twice */
  461. error = gfs2_change_nlink(ip, -1);
  462. if (error)
  463. return error;
  464. error = gfs2_change_nlink(ip, -1);
  465. if (error)
  466. return error;
  467. return error;
  468. }
  469. /**
  470. * gfs2_rmdir - Remove a directory
  471. * @dir: The parent directory of the directory to be removed
  472. * @dentry: The dentry of the directory to remove
  473. *
  474. * Remove a directory. Call gfs2_rmdiri()
  475. *
  476. * Returns: errno
  477. */
  478. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  479. {
  480. struct gfs2_inode *dip = GFS2_I(dir);
  481. struct gfs2_sbd *sdp = GFS2_SB(dir);
  482. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  483. struct gfs2_holder ghs[3];
  484. struct gfs2_rgrpd *rgd;
  485. struct gfs2_holder ri_gh;
  486. int error;
  487. error = gfs2_rindex_hold(sdp, &ri_gh);
  488. if (error)
  489. return error;
  490. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  491. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  492. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  493. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  494. error = gfs2_glock_nq(ghs); /* parent */
  495. if (error)
  496. goto out_parent;
  497. error = gfs2_glock_nq(ghs + 1); /* child */
  498. if (error)
  499. goto out_child;
  500. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  501. if (error)
  502. goto out_rgrp;
  503. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  504. if (error)
  505. goto out_gunlock;
  506. if (ip->i_entries < 2) {
  507. if (gfs2_consist_inode(ip))
  508. gfs2_dinode_print(ip);
  509. error = -EIO;
  510. goto out_gunlock;
  511. }
  512. if (ip->i_entries > 2) {
  513. error = -ENOTEMPTY;
  514. goto out_gunlock;
  515. }
  516. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  517. if (error)
  518. goto out_gunlock;
  519. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  520. gfs2_trans_end(sdp);
  521. out_gunlock:
  522. gfs2_glock_dq(ghs + 2);
  523. out_rgrp:
  524. gfs2_holder_uninit(ghs + 2);
  525. gfs2_glock_dq(ghs + 1);
  526. out_child:
  527. gfs2_holder_uninit(ghs + 1);
  528. gfs2_glock_dq(ghs);
  529. out_parent:
  530. gfs2_holder_uninit(ghs);
  531. gfs2_glock_dq_uninit(&ri_gh);
  532. return error;
  533. }
  534. /**
  535. * gfs2_mknod - Make a special file
  536. * @dir: The directory in which the special file will reside
  537. * @dentry: The dentry of the special file
  538. * @mode: The mode of the special file
  539. * @rdev: The device specification of the special file
  540. *
  541. */
  542. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  543. dev_t dev)
  544. {
  545. struct gfs2_inode *dip = GFS2_I(dir);
  546. struct gfs2_sbd *sdp = GFS2_SB(dir);
  547. struct gfs2_holder ghs[2];
  548. struct inode *inode;
  549. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  550. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  551. if (IS_ERR(inode)) {
  552. gfs2_holder_uninit(ghs);
  553. return PTR_ERR(inode);
  554. }
  555. gfs2_trans_end(sdp);
  556. if (dip->i_alloc->al_rgd)
  557. gfs2_inplace_release(dip);
  558. gfs2_quota_unlock(dip);
  559. gfs2_alloc_put(dip);
  560. gfs2_glock_dq_uninit_m(2, ghs);
  561. d_instantiate(dentry, inode);
  562. mark_inode_dirty(inode);
  563. return 0;
  564. }
  565. /*
  566. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  567. * @this: move this
  568. * @to: to here
  569. *
  570. * Follow @to back to the root and make sure we don't encounter @this
  571. * Assumes we already hold the rename lock.
  572. *
  573. * Returns: errno
  574. */
  575. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  576. {
  577. struct inode *dir = &to->i_inode;
  578. struct super_block *sb = dir->i_sb;
  579. struct inode *tmp;
  580. struct qstr dotdot;
  581. int error = 0;
  582. gfs2_str2qstr(&dotdot, "..");
  583. igrab(dir);
  584. for (;;) {
  585. if (dir == &this->i_inode) {
  586. error = -EINVAL;
  587. break;
  588. }
  589. if (dir == sb->s_root->d_inode) {
  590. error = 0;
  591. break;
  592. }
  593. tmp = gfs2_lookupi(dir, &dotdot, 1);
  594. if (IS_ERR(tmp)) {
  595. error = PTR_ERR(tmp);
  596. break;
  597. }
  598. iput(dir);
  599. dir = tmp;
  600. }
  601. iput(dir);
  602. return error;
  603. }
  604. /**
  605. * gfs2_rename - Rename a file
  606. * @odir: Parent directory of old file name
  607. * @odentry: The old dentry of the file
  608. * @ndir: Parent directory of new file name
  609. * @ndentry: The new dentry of the file
  610. *
  611. * Returns: errno
  612. */
  613. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  614. struct inode *ndir, struct dentry *ndentry)
  615. {
  616. struct gfs2_inode *odip = GFS2_I(odir);
  617. struct gfs2_inode *ndip = GFS2_I(ndir);
  618. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  619. struct gfs2_inode *nip = NULL;
  620. struct gfs2_sbd *sdp = GFS2_SB(odir);
  621. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  622. struct gfs2_rgrpd *nrgd;
  623. unsigned int num_gh;
  624. int dir_rename = 0;
  625. int alloc_required;
  626. unsigned int x;
  627. int error;
  628. if (ndentry->d_inode) {
  629. nip = GFS2_I(ndentry->d_inode);
  630. if (ip == nip)
  631. return 0;
  632. }
  633. if (odip != ndip) {
  634. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  635. 0, &r_gh);
  636. if (error)
  637. goto out;
  638. if (S_ISDIR(ip->i_inode.i_mode)) {
  639. dir_rename = 1;
  640. /* don't move a dirctory into it's subdir */
  641. error = gfs2_ok_to_move(ip, ndip);
  642. if (error)
  643. goto out_gunlock_r;
  644. }
  645. }
  646. num_gh = 1;
  647. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  648. if (odip != ndip) {
  649. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  650. num_gh++;
  651. }
  652. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  653. num_gh++;
  654. if (nip) {
  655. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  656. num_gh++;
  657. /* grab the resource lock for unlink flag twiddling
  658. * this is the case of the target file already existing
  659. * so we unlink before doing the rename
  660. */
  661. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  662. if (nrgd)
  663. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  664. }
  665. for (x = 0; x < num_gh; x++) {
  666. error = gfs2_glock_nq(ghs + x);
  667. if (error)
  668. goto out_gunlock;
  669. }
  670. /* Check out the old directory */
  671. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  672. if (error)
  673. goto out_gunlock;
  674. /* Check out the new directory */
  675. if (nip) {
  676. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  677. if (error)
  678. goto out_gunlock;
  679. if (S_ISDIR(nip->i_inode.i_mode)) {
  680. if (nip->i_entries < 2) {
  681. if (gfs2_consist_inode(nip))
  682. gfs2_dinode_print(nip);
  683. error = -EIO;
  684. goto out_gunlock;
  685. }
  686. if (nip->i_entries > 2) {
  687. error = -ENOTEMPTY;
  688. goto out_gunlock;
  689. }
  690. }
  691. } else {
  692. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  693. if (error)
  694. goto out_gunlock;
  695. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  696. switch (error) {
  697. case -ENOENT:
  698. error = 0;
  699. break;
  700. case 0:
  701. error = -EEXIST;
  702. default:
  703. goto out_gunlock;
  704. };
  705. if (odip != ndip) {
  706. if (!ndip->i_inode.i_nlink) {
  707. error = -EINVAL;
  708. goto out_gunlock;
  709. }
  710. if (ndip->i_entries == (u32)-1) {
  711. error = -EFBIG;
  712. goto out_gunlock;
  713. }
  714. if (S_ISDIR(ip->i_inode.i_mode) &&
  715. ndip->i_inode.i_nlink == (u32)-1) {
  716. error = -EMLINK;
  717. goto out_gunlock;
  718. }
  719. }
  720. }
  721. /* Check out the dir to be renamed */
  722. if (dir_rename) {
  723. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  724. if (error)
  725. goto out_gunlock;
  726. }
  727. alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  728. if (error < 0)
  729. goto out_gunlock;
  730. error = 0;
  731. if (alloc_required) {
  732. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  733. if (!al) {
  734. error = -ENOMEM;
  735. goto out_gunlock;
  736. }
  737. error = gfs2_quota_lock_check(ndip);
  738. if (error)
  739. goto out_alloc;
  740. al->al_requested = sdp->sd_max_dirres;
  741. error = gfs2_inplace_reserve(ndip);
  742. if (error)
  743. goto out_gunlock_q;
  744. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  745. al->al_rgd->rd_length +
  746. 4 * RES_DINODE + 4 * RES_LEAF +
  747. RES_STATFS + RES_QUOTA + 4, 0);
  748. if (error)
  749. goto out_ipreserv;
  750. } else {
  751. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  752. 5 * RES_LEAF + 4, 0);
  753. if (error)
  754. goto out_gunlock;
  755. }
  756. /* Remove the target file, if it exists */
  757. if (nip) {
  758. if (S_ISDIR(nip->i_inode.i_mode))
  759. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  760. else {
  761. error = gfs2_dir_del(ndip, &ndentry->d_name);
  762. if (error)
  763. goto out_end_trans;
  764. error = gfs2_change_nlink(nip, -1);
  765. }
  766. if (error)
  767. goto out_end_trans;
  768. }
  769. if (dir_rename) {
  770. struct qstr name;
  771. gfs2_str2qstr(&name, "..");
  772. error = gfs2_change_nlink(ndip, +1);
  773. if (error)
  774. goto out_end_trans;
  775. error = gfs2_change_nlink(odip, -1);
  776. if (error)
  777. goto out_end_trans;
  778. error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
  779. if (error)
  780. goto out_end_trans;
  781. } else {
  782. struct buffer_head *dibh;
  783. error = gfs2_meta_inode_buffer(ip, &dibh);
  784. if (error)
  785. goto out_end_trans;
  786. ip->i_inode.i_ctime = CURRENT_TIME;
  787. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  788. gfs2_dinode_out(ip, dibh->b_data);
  789. brelse(dibh);
  790. }
  791. error = gfs2_dir_del(odip, &odentry->d_name);
  792. if (error)
  793. goto out_end_trans;
  794. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  795. if (error)
  796. goto out_end_trans;
  797. out_end_trans:
  798. gfs2_trans_end(sdp);
  799. out_ipreserv:
  800. if (alloc_required)
  801. gfs2_inplace_release(ndip);
  802. out_gunlock_q:
  803. if (alloc_required)
  804. gfs2_quota_unlock(ndip);
  805. out_alloc:
  806. if (alloc_required)
  807. gfs2_alloc_put(ndip);
  808. out_gunlock:
  809. while (x--) {
  810. gfs2_glock_dq(ghs + x);
  811. gfs2_holder_uninit(ghs + x);
  812. }
  813. out_gunlock_r:
  814. if (r_gh.gh_gl)
  815. gfs2_glock_dq_uninit(&r_gh);
  816. out:
  817. return error;
  818. }
  819. /**
  820. * gfs2_readlinki - return the contents of a symlink
  821. * @ip: the symlink's inode
  822. * @buf: a pointer to the buffer to be filled
  823. * @len: a pointer to the length of @buf
  824. *
  825. * If @buf is too small, a piece of memory is kmalloc()ed and needs
  826. * to be freed by the caller.
  827. *
  828. * Returns: errno
  829. */
  830. static int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
  831. {
  832. struct gfs2_holder i_gh;
  833. struct buffer_head *dibh;
  834. unsigned int x;
  835. int error;
  836. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  837. error = gfs2_glock_nq(&i_gh);
  838. if (error) {
  839. gfs2_holder_uninit(&i_gh);
  840. return error;
  841. }
  842. if (!ip->i_disksize) {
  843. gfs2_consist_inode(ip);
  844. error = -EIO;
  845. goto out;
  846. }
  847. error = gfs2_meta_inode_buffer(ip, &dibh);
  848. if (error)
  849. goto out;
  850. x = ip->i_disksize + 1;
  851. if (x > *len) {
  852. *buf = kmalloc(x, GFP_NOFS);
  853. if (!*buf) {
  854. error = -ENOMEM;
  855. goto out_brelse;
  856. }
  857. }
  858. memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  859. *len = x;
  860. out_brelse:
  861. brelse(dibh);
  862. out:
  863. gfs2_glock_dq_uninit(&i_gh);
  864. return error;
  865. }
  866. /**
  867. * gfs2_readlink - Read the value of a symlink
  868. * @dentry: the symlink
  869. * @buf: the buffer to read the symlink data into
  870. * @size: the size of the buffer
  871. *
  872. * Returns: errno
  873. */
  874. static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
  875. int user_size)
  876. {
  877. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  878. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  879. unsigned int len = GFS2_FAST_NAME_SIZE;
  880. int error;
  881. error = gfs2_readlinki(ip, &buf, &len);
  882. if (error)
  883. return error;
  884. if (user_size > len - 1)
  885. user_size = len - 1;
  886. if (copy_to_user(user_buf, buf, user_size))
  887. error = -EFAULT;
  888. else
  889. error = user_size;
  890. if (buf != array)
  891. kfree(buf);
  892. return error;
  893. }
  894. /**
  895. * gfs2_follow_link - Follow a symbolic link
  896. * @dentry: The dentry of the link
  897. * @nd: Data that we pass to vfs_follow_link()
  898. *
  899. * This can handle symlinks of any size. It is optimised for symlinks
  900. * under GFS2_FAST_NAME_SIZE.
  901. *
  902. * Returns: 0 on success or error code
  903. */
  904. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  905. {
  906. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  907. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  908. unsigned int len = GFS2_FAST_NAME_SIZE;
  909. int error;
  910. error = gfs2_readlinki(ip, &buf, &len);
  911. if (!error) {
  912. error = vfs_follow_link(nd, buf);
  913. if (buf != array)
  914. kfree(buf);
  915. }
  916. return ERR_PTR(error);
  917. }
  918. /**
  919. * gfs2_permission -
  920. * @inode:
  921. * @mask:
  922. * @nd: passed from Linux VFS, ignored by us
  923. *
  924. * This may be called from the VFS directly, or from within GFS2 with the
  925. * inode locked, so we look to see if the glock is already locked and only
  926. * lock the glock if its not already been done.
  927. *
  928. * Returns: errno
  929. */
  930. int gfs2_permission(struct inode *inode, int mask)
  931. {
  932. struct gfs2_inode *ip = GFS2_I(inode);
  933. struct gfs2_holder i_gh;
  934. int error;
  935. int unlock = 0;
  936. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  937. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  938. if (error)
  939. return error;
  940. unlock = 1;
  941. }
  942. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  943. error = -EACCES;
  944. else
  945. error = generic_permission(inode, mask, gfs2_check_acl);
  946. if (unlock)
  947. gfs2_glock_dq_uninit(&i_gh);
  948. return error;
  949. }
  950. static int setattr_size(struct inode *inode, struct iattr *attr)
  951. {
  952. struct gfs2_inode *ip = GFS2_I(inode);
  953. struct gfs2_sbd *sdp = GFS2_SB(inode);
  954. int error;
  955. if (attr->ia_size != ip->i_disksize) {
  956. error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
  957. if (error)
  958. return error;
  959. error = vmtruncate(inode, attr->ia_size);
  960. gfs2_trans_end(sdp);
  961. if (error)
  962. return error;
  963. }
  964. error = gfs2_truncatei(ip, attr->ia_size);
  965. if (error && (inode->i_size != ip->i_disksize))
  966. i_size_write(inode, ip->i_disksize);
  967. return error;
  968. }
  969. static int setattr_chown(struct inode *inode, struct iattr *attr)
  970. {
  971. struct gfs2_inode *ip = GFS2_I(inode);
  972. struct gfs2_sbd *sdp = GFS2_SB(inode);
  973. struct buffer_head *dibh;
  974. u32 ouid, ogid, nuid, ngid;
  975. int error;
  976. ouid = inode->i_uid;
  977. ogid = inode->i_gid;
  978. nuid = attr->ia_uid;
  979. ngid = attr->ia_gid;
  980. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  981. ouid = nuid = NO_QUOTA_CHANGE;
  982. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  983. ogid = ngid = NO_QUOTA_CHANGE;
  984. if (!gfs2_alloc_get(ip))
  985. return -ENOMEM;
  986. error = gfs2_quota_lock(ip, nuid, ngid);
  987. if (error)
  988. goto out_alloc;
  989. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  990. error = gfs2_quota_check(ip, nuid, ngid);
  991. if (error)
  992. goto out_gunlock_q;
  993. }
  994. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  995. if (error)
  996. goto out_gunlock_q;
  997. error = gfs2_meta_inode_buffer(ip, &dibh);
  998. if (error)
  999. goto out_end_trans;
  1000. error = inode_setattr(inode, attr);
  1001. gfs2_assert_warn(sdp, !error);
  1002. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1003. gfs2_dinode_out(ip, dibh->b_data);
  1004. brelse(dibh);
  1005. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1006. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1007. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1008. gfs2_quota_change(ip, blocks, nuid, ngid);
  1009. }
  1010. out_end_trans:
  1011. gfs2_trans_end(sdp);
  1012. out_gunlock_q:
  1013. gfs2_quota_unlock(ip);
  1014. out_alloc:
  1015. gfs2_alloc_put(ip);
  1016. return error;
  1017. }
  1018. /**
  1019. * gfs2_setattr - Change attributes on an inode
  1020. * @dentry: The dentry which is changing
  1021. * @attr: The structure describing the change
  1022. *
  1023. * The VFS layer wants to change one or more of an inodes attributes. Write
  1024. * that change out to disk.
  1025. *
  1026. * Returns: errno
  1027. */
  1028. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1029. {
  1030. struct inode *inode = dentry->d_inode;
  1031. struct gfs2_inode *ip = GFS2_I(inode);
  1032. struct gfs2_holder i_gh;
  1033. int error;
  1034. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1035. if (error)
  1036. return error;
  1037. error = -EPERM;
  1038. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1039. goto out;
  1040. error = inode_change_ok(inode, attr);
  1041. if (error)
  1042. goto out;
  1043. if (attr->ia_valid & ATTR_SIZE)
  1044. error = setattr_size(inode, attr);
  1045. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1046. error = setattr_chown(inode, attr);
  1047. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1048. error = gfs2_acl_chmod(ip, attr);
  1049. else
  1050. error = gfs2_setattr_simple(ip, attr);
  1051. out:
  1052. gfs2_glock_dq_uninit(&i_gh);
  1053. if (!error)
  1054. mark_inode_dirty(inode);
  1055. return error;
  1056. }
  1057. /**
  1058. * gfs2_getattr - Read out an inode's attributes
  1059. * @mnt: The vfsmount the inode is being accessed from
  1060. * @dentry: The dentry to stat
  1061. * @stat: The inode's stats
  1062. *
  1063. * This may be called from the VFS directly, or from within GFS2 with the
  1064. * inode locked, so we look to see if the glock is already locked and only
  1065. * lock the glock if its not already been done. Note that its the NFS
  1066. * readdirplus operation which causes this to be called (from filldir)
  1067. * with the glock already held.
  1068. *
  1069. * Returns: errno
  1070. */
  1071. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1072. struct kstat *stat)
  1073. {
  1074. struct inode *inode = dentry->d_inode;
  1075. struct gfs2_inode *ip = GFS2_I(inode);
  1076. struct gfs2_holder gh;
  1077. int error;
  1078. int unlock = 0;
  1079. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1080. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1081. if (error)
  1082. return error;
  1083. unlock = 1;
  1084. }
  1085. generic_fillattr(inode, stat);
  1086. if (unlock)
  1087. gfs2_glock_dq_uninit(&gh);
  1088. return 0;
  1089. }
  1090. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1091. const void *data, size_t size, int flags)
  1092. {
  1093. struct inode *inode = dentry->d_inode;
  1094. struct gfs2_ea_request er;
  1095. memset(&er, 0, sizeof(struct gfs2_ea_request));
  1096. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  1097. if (er.er_type == GFS2_EATYPE_UNUSED)
  1098. return -EOPNOTSUPP;
  1099. er.er_data = (char *)data;
  1100. er.er_name_len = strlen(er.er_name);
  1101. er.er_data_len = size;
  1102. er.er_flags = flags;
  1103. gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
  1104. return gfs2_ea_set(GFS2_I(inode), &er);
  1105. }
  1106. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1107. void *data, size_t size)
  1108. {
  1109. struct gfs2_ea_request er;
  1110. memset(&er, 0, sizeof(struct gfs2_ea_request));
  1111. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  1112. if (er.er_type == GFS2_EATYPE_UNUSED)
  1113. return -EOPNOTSUPP;
  1114. er.er_data = data;
  1115. er.er_name_len = strlen(er.er_name);
  1116. er.er_data_len = size;
  1117. return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
  1118. }
  1119. static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  1120. {
  1121. struct gfs2_ea_request er;
  1122. memset(&er, 0, sizeof(struct gfs2_ea_request));
  1123. er.er_data = (size) ? buffer : NULL;
  1124. er.er_data_len = size;
  1125. return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
  1126. }
  1127. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1128. {
  1129. struct gfs2_ea_request er;
  1130. memset(&er, 0, sizeof(struct gfs2_ea_request));
  1131. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  1132. if (er.er_type == GFS2_EATYPE_UNUSED)
  1133. return -EOPNOTSUPP;
  1134. er.er_name_len = strlen(er.er_name);
  1135. return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
  1136. }
  1137. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1138. u64 start, u64 len)
  1139. {
  1140. struct gfs2_inode *ip = GFS2_I(inode);
  1141. struct gfs2_holder gh;
  1142. int ret;
  1143. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1144. if (ret)
  1145. return ret;
  1146. mutex_lock(&inode->i_mutex);
  1147. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1148. if (ret)
  1149. goto out;
  1150. if (gfs2_is_stuffed(ip)) {
  1151. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1152. u64 size = i_size_read(inode);
  1153. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1154. FIEMAP_EXTENT_DATA_INLINE;
  1155. phys += sizeof(struct gfs2_dinode);
  1156. phys += start;
  1157. if (start + len > size)
  1158. len = size - start;
  1159. if (start < size)
  1160. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1161. len, flags);
  1162. if (ret == 1)
  1163. ret = 0;
  1164. } else {
  1165. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1166. gfs2_block_map);
  1167. }
  1168. gfs2_glock_dq_uninit(&gh);
  1169. out:
  1170. mutex_unlock(&inode->i_mutex);
  1171. return ret;
  1172. }
  1173. const struct inode_operations gfs2_file_iops = {
  1174. .permission = gfs2_permission,
  1175. .setattr = gfs2_setattr,
  1176. .getattr = gfs2_getattr,
  1177. .setxattr = gfs2_setxattr,
  1178. .getxattr = gfs2_getxattr,
  1179. .listxattr = gfs2_listxattr,
  1180. .removexattr = gfs2_removexattr,
  1181. .fiemap = gfs2_fiemap,
  1182. };
  1183. const struct inode_operations gfs2_dir_iops = {
  1184. .create = gfs2_create,
  1185. .lookup = gfs2_lookup,
  1186. .link = gfs2_link,
  1187. .unlink = gfs2_unlink,
  1188. .symlink = gfs2_symlink,
  1189. .mkdir = gfs2_mkdir,
  1190. .rmdir = gfs2_rmdir,
  1191. .mknod = gfs2_mknod,
  1192. .rename = gfs2_rename,
  1193. .permission = gfs2_permission,
  1194. .setattr = gfs2_setattr,
  1195. .getattr = gfs2_getattr,
  1196. .setxattr = gfs2_setxattr,
  1197. .getxattr = gfs2_getxattr,
  1198. .listxattr = gfs2_listxattr,
  1199. .removexattr = gfs2_removexattr,
  1200. .fiemap = gfs2_fiemap,
  1201. };
  1202. const struct inode_operations gfs2_symlink_iops = {
  1203. .readlink = gfs2_readlink,
  1204. .follow_link = gfs2_follow_link,
  1205. .permission = gfs2_permission,
  1206. .setattr = gfs2_setattr,
  1207. .getattr = gfs2_getattr,
  1208. .setxattr = gfs2_setxattr,
  1209. .getxattr = gfs2_getxattr,
  1210. .listxattr = gfs2_listxattr,
  1211. .removexattr = gfs2_removexattr,
  1212. .fiemap = gfs2_fiemap,
  1213. };