ops_inode.c 31 KB

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