ops_inode.c 31 KB

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