inode.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2011 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 <linux/security.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 "xattr.h"
  28. #include "glock.h"
  29. #include "inode.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. #include "super.h"
  36. #include "glops.h"
  37. struct gfs2_skip_data {
  38. u64 no_addr;
  39. int skipped;
  40. int non_block;
  41. };
  42. static int iget_test(struct inode *inode, void *opaque)
  43. {
  44. struct gfs2_inode *ip = GFS2_I(inode);
  45. struct gfs2_skip_data *data = opaque;
  46. if (ip->i_no_addr == data->no_addr) {
  47. if (data->non_block &&
  48. inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  49. data->skipped = 1;
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int iget_set(struct inode *inode, void *opaque)
  57. {
  58. struct gfs2_inode *ip = GFS2_I(inode);
  59. struct gfs2_skip_data *data = opaque;
  60. if (data->skipped)
  61. return -ENOENT;
  62. inode->i_ino = (unsigned long)(data->no_addr);
  63. ip->i_no_addr = data->no_addr;
  64. return 0;
  65. }
  66. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
  67. {
  68. unsigned long hash = (unsigned long)no_addr;
  69. struct gfs2_skip_data data;
  70. data.no_addr = no_addr;
  71. data.skipped = 0;
  72. data.non_block = non_block;
  73. return ilookup5(sb, hash, iget_test, &data);
  74. }
  75. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
  76. int non_block)
  77. {
  78. struct gfs2_skip_data data;
  79. unsigned long hash = (unsigned long)no_addr;
  80. data.no_addr = no_addr;
  81. data.skipped = 0;
  82. data.non_block = non_block;
  83. return iget5_locked(sb, hash, iget_test, iget_set, &data);
  84. }
  85. /**
  86. * gfs2_set_iop - Sets inode operations
  87. * @inode: The inode with correct i_mode filled in
  88. *
  89. * GFS2 lookup code fills in vfs inode contents based on info obtained
  90. * from directory entry inside gfs2_inode_lookup().
  91. */
  92. static void gfs2_set_iop(struct inode *inode)
  93. {
  94. struct gfs2_sbd *sdp = GFS2_SB(inode);
  95. umode_t mode = inode->i_mode;
  96. if (S_ISREG(mode)) {
  97. inode->i_op = &gfs2_file_iops;
  98. if (gfs2_localflocks(sdp))
  99. inode->i_fop = &gfs2_file_fops_nolock;
  100. else
  101. inode->i_fop = &gfs2_file_fops;
  102. } else if (S_ISDIR(mode)) {
  103. inode->i_op = &gfs2_dir_iops;
  104. if (gfs2_localflocks(sdp))
  105. inode->i_fop = &gfs2_dir_fops_nolock;
  106. else
  107. inode->i_fop = &gfs2_dir_fops;
  108. } else if (S_ISLNK(mode)) {
  109. inode->i_op = &gfs2_symlink_iops;
  110. } else {
  111. inode->i_op = &gfs2_file_iops;
  112. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  113. }
  114. }
  115. /**
  116. * gfs2_inode_lookup - Lookup an inode
  117. * @sb: The super block
  118. * @no_addr: The inode number
  119. * @type: The type of the inode
  120. * non_block: Can we block on inodes that are being freed?
  121. *
  122. * Returns: A VFS inode, or an error
  123. */
  124. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  125. u64 no_addr, u64 no_formal_ino, int non_block)
  126. {
  127. struct inode *inode;
  128. struct gfs2_inode *ip;
  129. struct gfs2_glock *io_gl = NULL;
  130. int error;
  131. inode = gfs2_iget(sb, no_addr, non_block);
  132. ip = GFS2_I(inode);
  133. if (!inode)
  134. return ERR_PTR(-ENOMEM);
  135. if (inode->i_state & I_NEW) {
  136. struct gfs2_sbd *sdp = GFS2_SB(inode);
  137. ip->i_no_formal_ino = no_formal_ino;
  138. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  139. if (unlikely(error))
  140. goto fail;
  141. ip->i_gl->gl_object = ip;
  142. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  143. if (unlikely(error))
  144. goto fail_put;
  145. set_bit(GIF_INVALID, &ip->i_flags);
  146. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  147. if (unlikely(error))
  148. goto fail_iopen;
  149. ip->i_iopen_gh.gh_gl->gl_object = ip;
  150. gfs2_glock_put(io_gl);
  151. io_gl = NULL;
  152. if (type == DT_UNKNOWN) {
  153. /* Inode glock must be locked already */
  154. error = gfs2_inode_refresh(GFS2_I(inode));
  155. if (error)
  156. goto fail_refresh;
  157. } else {
  158. inode->i_mode = DT2IF(type);
  159. }
  160. gfs2_set_iop(inode);
  161. unlock_new_inode(inode);
  162. }
  163. return inode;
  164. fail_refresh:
  165. ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
  166. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  167. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  168. fail_iopen:
  169. if (io_gl)
  170. gfs2_glock_put(io_gl);
  171. fail_put:
  172. ip->i_gl->gl_object = NULL;
  173. gfs2_glock_put(ip->i_gl);
  174. fail:
  175. iget_failed(inode);
  176. return ERR_PTR(error);
  177. }
  178. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  179. u64 *no_formal_ino, unsigned int blktype)
  180. {
  181. struct super_block *sb = sdp->sd_vfs;
  182. struct gfs2_holder i_gh;
  183. struct inode *inode = NULL;
  184. int error;
  185. /* Must not read in block until block type is verified */
  186. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  187. LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
  188. if (error)
  189. return ERR_PTR(error);
  190. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  191. if (error)
  192. goto fail;
  193. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
  194. if (IS_ERR(inode))
  195. goto fail;
  196. /* Two extra checks for NFS only */
  197. if (no_formal_ino) {
  198. error = -ESTALE;
  199. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  200. goto fail_iput;
  201. error = -EIO;
  202. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  203. goto fail_iput;
  204. error = 0;
  205. }
  206. fail:
  207. gfs2_glock_dq_uninit(&i_gh);
  208. return error ? ERR_PTR(error) : inode;
  209. fail_iput:
  210. iput(inode);
  211. goto fail;
  212. }
  213. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  214. {
  215. struct qstr qstr;
  216. struct inode *inode;
  217. gfs2_str2qstr(&qstr, name);
  218. inode = gfs2_lookupi(dip, &qstr, 1);
  219. /* gfs2_lookupi has inconsistent callers: vfs
  220. * related routines expect NULL for no entry found,
  221. * gfs2_lookup_simple callers expect ENOENT
  222. * and do not check for NULL.
  223. */
  224. if (inode == NULL)
  225. return ERR_PTR(-ENOENT);
  226. else
  227. return inode;
  228. }
  229. /**
  230. * gfs2_lookupi - Look up a filename in a directory and return its inode
  231. * @d_gh: An initialized holder for the directory glock
  232. * @name: The name of the inode to look for
  233. * @is_root: If 1, ignore the caller's permissions
  234. * @i_gh: An uninitialized holder for the new inode glock
  235. *
  236. * This can be called via the VFS filldir function when NFS is doing
  237. * a readdirplus and the inode which its intending to stat isn't
  238. * already in cache. In this case we must not take the directory glock
  239. * again, since the readdir call will have already taken that lock.
  240. *
  241. * Returns: errno
  242. */
  243. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  244. int is_root)
  245. {
  246. struct super_block *sb = dir->i_sb;
  247. struct gfs2_inode *dip = GFS2_I(dir);
  248. struct gfs2_holder d_gh;
  249. int error = 0;
  250. struct inode *inode = NULL;
  251. int unlock = 0;
  252. if (!name->len || name->len > GFS2_FNAMESIZE)
  253. return ERR_PTR(-ENAMETOOLONG);
  254. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  255. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  256. dir == sb->s_root->d_inode)) {
  257. igrab(dir);
  258. return dir;
  259. }
  260. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  261. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  262. if (error)
  263. return ERR_PTR(error);
  264. unlock = 1;
  265. }
  266. if (!is_root) {
  267. error = gfs2_permission(dir, MAY_EXEC);
  268. if (error)
  269. goto out;
  270. }
  271. inode = gfs2_dir_search(dir, name, false);
  272. if (IS_ERR(inode))
  273. error = PTR_ERR(inode);
  274. out:
  275. if (unlock)
  276. gfs2_glock_dq_uninit(&d_gh);
  277. if (error == -ENOENT)
  278. return NULL;
  279. return inode ? inode : ERR_PTR(error);
  280. }
  281. /**
  282. * create_ok - OK to create a new on-disk inode here?
  283. * @dip: Directory in which dinode is to be created
  284. * @name: Name of new dinode
  285. * @mode:
  286. *
  287. * Returns: errno
  288. */
  289. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  290. umode_t mode)
  291. {
  292. int error;
  293. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  294. if (error)
  295. return error;
  296. /* Don't create entries in an unlinked directory */
  297. if (!dip->i_inode.i_nlink)
  298. return -ENOENT;
  299. if (dip->i_entries == (u32)-1)
  300. return -EFBIG;
  301. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  302. return -EMLINK;
  303. return 0;
  304. }
  305. static void munge_mode_uid_gid(const struct gfs2_inode *dip,
  306. struct inode *inode)
  307. {
  308. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  309. (dip->i_inode.i_mode & S_ISUID) &&
  310. !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
  311. if (S_ISDIR(inode->i_mode))
  312. inode->i_mode |= S_ISUID;
  313. else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
  314. inode->i_mode &= ~07111;
  315. inode->i_uid = dip->i_inode.i_uid;
  316. } else
  317. inode->i_uid = current_fsuid();
  318. if (dip->i_inode.i_mode & S_ISGID) {
  319. if (S_ISDIR(inode->i_mode))
  320. inode->i_mode |= S_ISGID;
  321. inode->i_gid = dip->i_inode.i_gid;
  322. } else
  323. inode->i_gid = current_fsgid();
  324. }
  325. static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
  326. {
  327. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  328. struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
  329. int error;
  330. error = gfs2_quota_lock_check(ip);
  331. if (error)
  332. goto out;
  333. error = gfs2_inplace_reserve(ip, &ap);
  334. if (error)
  335. goto out_quota;
  336. error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
  337. if (error)
  338. goto out_ipreserv;
  339. error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
  340. ip->i_no_formal_ino = ip->i_generation;
  341. ip->i_inode.i_ino = ip->i_no_addr;
  342. ip->i_goal = ip->i_no_addr;
  343. gfs2_trans_end(sdp);
  344. out_ipreserv:
  345. gfs2_inplace_release(ip);
  346. out_quota:
  347. gfs2_quota_unlock(ip);
  348. out:
  349. return error;
  350. }
  351. static void gfs2_init_dir(struct buffer_head *dibh,
  352. const struct gfs2_inode *parent)
  353. {
  354. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  355. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  356. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  357. dent->de_inum = di->di_num; /* already GFS2 endian */
  358. dent->de_type = cpu_to_be16(DT_DIR);
  359. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  360. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  361. gfs2_inum_out(parent, dent);
  362. dent->de_type = cpu_to_be16(DT_DIR);
  363. }
  364. /**
  365. * gfs2_init_xattr - Initialise an xattr block for a new inode
  366. * @ip: The inode in question
  367. *
  368. * This sets up an empty xattr block for a new inode, ready to
  369. * take any ACLs, LSM xattrs, etc.
  370. */
  371. static void gfs2_init_xattr(struct gfs2_inode *ip)
  372. {
  373. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  374. struct buffer_head *bh;
  375. struct gfs2_ea_header *ea;
  376. bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
  377. gfs2_trans_add_meta(ip->i_gl, bh);
  378. gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  379. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  380. ea = GFS2_EA_BH2FIRST(bh);
  381. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  382. ea->ea_type = GFS2_EATYPE_UNUSED;
  383. ea->ea_flags = GFS2_EAFLAG_LAST;
  384. brelse(bh);
  385. }
  386. /**
  387. * init_dinode - Fill in a new dinode structure
  388. * @dip: The directory this inode is being created in
  389. * @ip: The inode
  390. * @symname: The symlink destination (if a symlink)
  391. * @bhp: The buffer head (returned to caller)
  392. *
  393. */
  394. static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  395. const char *symname)
  396. {
  397. struct gfs2_dinode *di;
  398. struct buffer_head *dibh;
  399. dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
  400. gfs2_trans_add_meta(ip->i_gl, dibh);
  401. di = (struct gfs2_dinode *)dibh->b_data;
  402. gfs2_dinode_out(ip, di);
  403. di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
  404. di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
  405. di->__pad1 = 0;
  406. di->__pad2 = 0;
  407. di->__pad3 = 0;
  408. memset(&di->__pad4, 0, sizeof(di->__pad4));
  409. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  410. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  411. switch(ip->i_inode.i_mode & S_IFMT) {
  412. case S_IFDIR:
  413. gfs2_init_dir(dibh, dip);
  414. break;
  415. case S_IFLNK:
  416. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
  417. break;
  418. }
  419. set_buffer_uptodate(dibh);
  420. brelse(dibh);
  421. }
  422. /**
  423. * gfs2_trans_da_blocks - Calculate number of blocks to link inode
  424. * @dip: The directory we are linking into
  425. * @da: The dir add information
  426. * @nr_inodes: The number of inodes involved
  427. *
  428. * This calculate the number of blocks we need to reserve in a
  429. * transaction to link @nr_inodes into a directory. In most cases
  430. * @nr_inodes will be 2 (the directory plus the inode being linked in)
  431. * but in case of rename, 4 may be required.
  432. *
  433. * Returns: Number of blocks
  434. */
  435. static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
  436. const struct gfs2_diradd *da,
  437. unsigned nr_inodes)
  438. {
  439. return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
  440. (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
  441. }
  442. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  443. struct gfs2_inode *ip, struct gfs2_diradd *da)
  444. {
  445. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  446. struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
  447. int error;
  448. if (da->nr_blocks) {
  449. error = gfs2_quota_lock_check(dip);
  450. if (error)
  451. goto fail_quota_locks;
  452. error = gfs2_inplace_reserve(dip, &ap);
  453. if (error)
  454. goto fail_quota_locks;
  455. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
  456. if (error)
  457. goto fail_ipreserv;
  458. } else {
  459. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  460. if (error)
  461. goto fail_quota_locks;
  462. }
  463. error = gfs2_dir_add(&dip->i_inode, name, ip, da);
  464. if (error)
  465. goto fail_end_trans;
  466. fail_end_trans:
  467. gfs2_trans_end(sdp);
  468. fail_ipreserv:
  469. gfs2_inplace_release(dip);
  470. fail_quota_locks:
  471. gfs2_quota_unlock(dip);
  472. return error;
  473. }
  474. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  475. void *fs_info)
  476. {
  477. const struct xattr *xattr;
  478. int err = 0;
  479. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  480. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  481. xattr->value_len, 0,
  482. GFS2_EATYPE_SECURITY);
  483. if (err < 0)
  484. break;
  485. }
  486. return err;
  487. }
  488. /**
  489. * gfs2_create_inode - Create a new inode
  490. * @dir: The parent directory
  491. * @dentry: The new dentry
  492. * @file: If non-NULL, the file which is being opened
  493. * @mode: The permissions on the new inode
  494. * @dev: For device nodes, this is the device number
  495. * @symname: For symlinks, this is the link destination
  496. * @size: The initial size of the inode (ignored for directories)
  497. *
  498. * Returns: 0 on success, or error code
  499. */
  500. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  501. struct file *file,
  502. umode_t mode, dev_t dev, const char *symname,
  503. unsigned int size, int excl, int *opened)
  504. {
  505. const struct qstr *name = &dentry->d_name;
  506. struct posix_acl *default_acl, *acl;
  507. struct gfs2_holder ghs[2];
  508. struct inode *inode = NULL;
  509. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  510. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  511. struct gfs2_glock *io_gl;
  512. int error, free_vfs_inode = 0;
  513. u32 aflags = 0;
  514. unsigned blocks = 1;
  515. struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
  516. if (!name->len || name->len > GFS2_FNAMESIZE)
  517. return -ENAMETOOLONG;
  518. error = gfs2_rs_alloc(dip);
  519. if (error)
  520. return error;
  521. error = gfs2_rindex_update(sdp);
  522. if (error)
  523. return error;
  524. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  525. if (error)
  526. goto fail;
  527. error = create_ok(dip, name, mode);
  528. if (error)
  529. goto fail_gunlock;
  530. inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
  531. error = PTR_ERR(inode);
  532. if (!IS_ERR(inode)) {
  533. if (S_ISDIR(inode->i_mode)) {
  534. iput(inode);
  535. inode = ERR_PTR(-EISDIR);
  536. goto fail_gunlock;
  537. }
  538. d_instantiate(dentry, inode);
  539. error = 0;
  540. if (file) {
  541. if (S_ISREG(inode->i_mode))
  542. error = finish_open(file, dentry, gfs2_open_common, opened);
  543. else
  544. error = finish_no_open(file, NULL);
  545. }
  546. gfs2_glock_dq_uninit(ghs);
  547. return error;
  548. } else if (error != -ENOENT) {
  549. goto fail_gunlock;
  550. }
  551. error = gfs2_diradd_alloc_required(dir, name, &da);
  552. if (error < 0)
  553. goto fail_gunlock;
  554. inode = new_inode(sdp->sd_vfs);
  555. error = -ENOMEM;
  556. if (!inode)
  557. goto fail_gunlock;
  558. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  559. if (error)
  560. goto fail_free_vfs_inode;
  561. ip = GFS2_I(inode);
  562. error = gfs2_rs_alloc(ip);
  563. if (error)
  564. goto fail_free_acls;
  565. inode->i_mode = mode;
  566. set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
  567. inode->i_rdev = dev;
  568. inode->i_size = size;
  569. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  570. gfs2_set_inode_blocks(inode, 1);
  571. munge_mode_uid_gid(dip, inode);
  572. check_and_update_goal(dip);
  573. ip->i_goal = dip->i_goal;
  574. ip->i_diskflags = 0;
  575. ip->i_eattr = 0;
  576. ip->i_height = 0;
  577. ip->i_depth = 0;
  578. ip->i_entries = 0;
  579. switch(mode & S_IFMT) {
  580. case S_IFREG:
  581. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  582. gfs2_tune_get(sdp, gt_new_files_jdata))
  583. ip->i_diskflags |= GFS2_DIF_JDATA;
  584. gfs2_set_aops(inode);
  585. break;
  586. case S_IFDIR:
  587. ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
  588. ip->i_diskflags |= GFS2_DIF_JDATA;
  589. ip->i_entries = 2;
  590. break;
  591. }
  592. gfs2_set_inode_flags(inode);
  593. if ((GFS2_I(sdp->sd_root_dir->d_inode) == dip) ||
  594. (dip->i_diskflags & GFS2_DIF_TOPDIR))
  595. aflags |= GFS2_AF_ORLOV;
  596. if (default_acl || acl)
  597. blocks++;
  598. error = alloc_dinode(ip, aflags, &blocks);
  599. if (error)
  600. goto fail_free_inode;
  601. gfs2_set_inode_blocks(inode, blocks);
  602. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  603. if (error)
  604. goto fail_free_inode;
  605. ip->i_gl->gl_object = ip;
  606. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  607. if (error)
  608. goto fail_free_inode;
  609. error = gfs2_trans_begin(sdp, blocks, 0);
  610. if (error)
  611. goto fail_gunlock2;
  612. if (blocks > 1) {
  613. ip->i_eattr = ip->i_no_addr + 1;
  614. gfs2_init_xattr(ip);
  615. }
  616. init_dinode(dip, ip, symname);
  617. gfs2_trans_end(sdp);
  618. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  619. if (error)
  620. goto fail_gunlock2;
  621. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  622. if (error)
  623. goto fail_gunlock2;
  624. ip->i_iopen_gh.gh_gl->gl_object = ip;
  625. gfs2_glock_put(io_gl);
  626. gfs2_set_iop(inode);
  627. insert_inode_hash(inode);
  628. if (default_acl) {
  629. error = gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  630. posix_acl_release(default_acl);
  631. }
  632. if (acl) {
  633. if (!error)
  634. error = gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
  635. posix_acl_release(acl);
  636. }
  637. if (error)
  638. goto fail_gunlock3;
  639. error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
  640. &gfs2_initxattrs, NULL);
  641. if (error)
  642. goto fail_gunlock3;
  643. error = link_dinode(dip, name, ip, &da);
  644. if (error)
  645. goto fail_gunlock3;
  646. mark_inode_dirty(inode);
  647. d_instantiate(dentry, inode);
  648. if (file) {
  649. *opened |= FILE_CREATED;
  650. error = finish_open(file, dentry, gfs2_open_common, opened);
  651. }
  652. gfs2_glock_dq_uninit(ghs);
  653. gfs2_glock_dq_uninit(ghs + 1);
  654. return error;
  655. fail_gunlock3:
  656. gfs2_glock_dq_uninit(ghs + 1);
  657. if (ip->i_gl)
  658. gfs2_glock_put(ip->i_gl);
  659. goto fail_gunlock;
  660. fail_gunlock2:
  661. gfs2_glock_dq_uninit(ghs + 1);
  662. fail_free_inode:
  663. if (ip->i_gl)
  664. gfs2_glock_put(ip->i_gl);
  665. gfs2_rs_delete(ip, NULL);
  666. fail_free_acls:
  667. if (default_acl)
  668. posix_acl_release(default_acl);
  669. if (acl)
  670. posix_acl_release(acl);
  671. fail_free_vfs_inode:
  672. free_vfs_inode = 1;
  673. fail_gunlock:
  674. gfs2_dir_no_add(&da);
  675. gfs2_glock_dq_uninit(ghs);
  676. if (inode && !IS_ERR(inode)) {
  677. clear_nlink(inode);
  678. if (!free_vfs_inode)
  679. mark_inode_dirty(inode);
  680. set_bit(free_vfs_inode ? GIF_FREE_VFS_INODE : GIF_ALLOC_FAILED,
  681. &GFS2_I(inode)->i_flags);
  682. iput(inode);
  683. }
  684. fail:
  685. return error;
  686. }
  687. /**
  688. * gfs2_create - Create a file
  689. * @dir: The directory in which to create the file
  690. * @dentry: The dentry of the new file
  691. * @mode: The mode of the new file
  692. *
  693. * Returns: errno
  694. */
  695. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  696. umode_t mode, bool excl)
  697. {
  698. return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl, NULL);
  699. }
  700. /**
  701. * __gfs2_lookup - Look up a filename in a directory and return its inode
  702. * @dir: The directory inode
  703. * @dentry: The dentry of the new inode
  704. * @file: File to be opened
  705. * @opened: atomic_open flags
  706. *
  707. *
  708. * Returns: errno
  709. */
  710. static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
  711. struct file *file, int *opened)
  712. {
  713. struct inode *inode;
  714. struct dentry *d;
  715. struct gfs2_holder gh;
  716. struct gfs2_glock *gl;
  717. int error;
  718. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  719. if (inode == NULL) {
  720. d_add(dentry, NULL);
  721. return NULL;
  722. }
  723. if (IS_ERR(inode))
  724. return ERR_CAST(inode);
  725. gl = GFS2_I(inode)->i_gl;
  726. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  727. if (error) {
  728. iput(inode);
  729. return ERR_PTR(error);
  730. }
  731. d = d_splice_alias(inode, dentry);
  732. if (IS_ERR(d)) {
  733. gfs2_glock_dq_uninit(&gh);
  734. return d;
  735. }
  736. if (file && S_ISREG(inode->i_mode))
  737. error = finish_open(file, dentry, gfs2_open_common, opened);
  738. gfs2_glock_dq_uninit(&gh);
  739. if (error) {
  740. dput(d);
  741. return ERR_PTR(error);
  742. }
  743. return d;
  744. }
  745. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  746. unsigned flags)
  747. {
  748. return __gfs2_lookup(dir, dentry, NULL, NULL);
  749. }
  750. /**
  751. * gfs2_link - Link to a file
  752. * @old_dentry: The inode to link
  753. * @dir: Add link to this directory
  754. * @dentry: The name of the link
  755. *
  756. * Link the inode in "old_dentry" into the directory "dir" with the
  757. * name in "dentry".
  758. *
  759. * Returns: errno
  760. */
  761. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  762. struct dentry *dentry)
  763. {
  764. struct gfs2_inode *dip = GFS2_I(dir);
  765. struct gfs2_sbd *sdp = GFS2_SB(dir);
  766. struct inode *inode = old_dentry->d_inode;
  767. struct gfs2_inode *ip = GFS2_I(inode);
  768. struct gfs2_holder ghs[2];
  769. struct buffer_head *dibh;
  770. struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
  771. int error;
  772. if (S_ISDIR(inode->i_mode))
  773. return -EPERM;
  774. error = gfs2_rs_alloc(dip);
  775. if (error)
  776. return error;
  777. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  778. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  779. error = gfs2_glock_nq(ghs); /* parent */
  780. if (error)
  781. goto out_parent;
  782. error = gfs2_glock_nq(ghs + 1); /* child */
  783. if (error)
  784. goto out_child;
  785. error = -ENOENT;
  786. if (inode->i_nlink == 0)
  787. goto out_gunlock;
  788. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  789. if (error)
  790. goto out_gunlock;
  791. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  792. switch (error) {
  793. case -ENOENT:
  794. break;
  795. case 0:
  796. error = -EEXIST;
  797. default:
  798. goto out_gunlock;
  799. }
  800. error = -EINVAL;
  801. if (!dip->i_inode.i_nlink)
  802. goto out_gunlock;
  803. error = -EFBIG;
  804. if (dip->i_entries == (u32)-1)
  805. goto out_gunlock;
  806. error = -EPERM;
  807. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  808. goto out_gunlock;
  809. error = -EINVAL;
  810. if (!ip->i_inode.i_nlink)
  811. goto out_gunlock;
  812. error = -EMLINK;
  813. if (ip->i_inode.i_nlink == (u32)-1)
  814. goto out_gunlock;
  815. error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
  816. if (error < 0)
  817. goto out_gunlock;
  818. if (da.nr_blocks) {
  819. struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
  820. error = gfs2_quota_lock_check(dip);
  821. if (error)
  822. goto out_gunlock;
  823. error = gfs2_inplace_reserve(dip, &ap);
  824. if (error)
  825. goto out_gunlock_q;
  826. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
  827. if (error)
  828. goto out_ipres;
  829. } else {
  830. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  831. if (error)
  832. goto out_ipres;
  833. }
  834. error = gfs2_meta_inode_buffer(ip, &dibh);
  835. if (error)
  836. goto out_end_trans;
  837. error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
  838. if (error)
  839. goto out_brelse;
  840. gfs2_trans_add_meta(ip->i_gl, dibh);
  841. inc_nlink(&ip->i_inode);
  842. ip->i_inode.i_ctime = CURRENT_TIME;
  843. ihold(inode);
  844. d_instantiate(dentry, inode);
  845. mark_inode_dirty(inode);
  846. out_brelse:
  847. brelse(dibh);
  848. out_end_trans:
  849. gfs2_trans_end(sdp);
  850. out_ipres:
  851. if (da.nr_blocks)
  852. gfs2_inplace_release(dip);
  853. out_gunlock_q:
  854. if (da.nr_blocks)
  855. gfs2_quota_unlock(dip);
  856. out_gunlock:
  857. gfs2_dir_no_add(&da);
  858. gfs2_glock_dq(ghs + 1);
  859. out_child:
  860. gfs2_glock_dq(ghs);
  861. out_parent:
  862. gfs2_holder_uninit(ghs);
  863. gfs2_holder_uninit(ghs + 1);
  864. return error;
  865. }
  866. /*
  867. * gfs2_unlink_ok - check to see that a inode is still in a directory
  868. * @dip: the directory
  869. * @name: the name of the file
  870. * @ip: the inode
  871. *
  872. * Assumes that the lock on (at least) @dip is held.
  873. *
  874. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  875. */
  876. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  877. const struct gfs2_inode *ip)
  878. {
  879. int error;
  880. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  881. return -EPERM;
  882. if ((dip->i_inode.i_mode & S_ISVTX) &&
  883. !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
  884. !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
  885. return -EPERM;
  886. if (IS_APPEND(&dip->i_inode))
  887. return -EPERM;
  888. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  889. if (error)
  890. return error;
  891. return gfs2_dir_check(&dip->i_inode, name, ip);
  892. }
  893. /**
  894. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  895. * @dip: The parent directory
  896. * @name: The name of the entry in the parent directory
  897. * @inode: The inode to be removed
  898. *
  899. * Called with all the locks and in a transaction. This will only be
  900. * called for a directory after it has been checked to ensure it is empty.
  901. *
  902. * Returns: 0 on success, or an error
  903. */
  904. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  905. const struct dentry *dentry)
  906. {
  907. struct inode *inode = dentry->d_inode;
  908. struct gfs2_inode *ip = GFS2_I(inode);
  909. int error;
  910. error = gfs2_dir_del(dip, dentry);
  911. if (error)
  912. return error;
  913. ip->i_entries = 0;
  914. inode->i_ctime = CURRENT_TIME;
  915. if (S_ISDIR(inode->i_mode))
  916. clear_nlink(inode);
  917. else
  918. drop_nlink(inode);
  919. mark_inode_dirty(inode);
  920. if (inode->i_nlink == 0)
  921. gfs2_unlink_di(inode);
  922. return 0;
  923. }
  924. /**
  925. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  926. * @dir: The inode of the directory containing the inode to unlink
  927. * @dentry: The file itself
  928. *
  929. * This routine uses the type of the inode as a flag to figure out
  930. * whether this is an unlink or an rmdir.
  931. *
  932. * Returns: errno
  933. */
  934. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  935. {
  936. struct gfs2_inode *dip = GFS2_I(dir);
  937. struct gfs2_sbd *sdp = GFS2_SB(dir);
  938. struct inode *inode = dentry->d_inode;
  939. struct gfs2_inode *ip = GFS2_I(inode);
  940. struct gfs2_holder ghs[3];
  941. struct gfs2_rgrpd *rgd;
  942. int error;
  943. error = gfs2_rindex_update(sdp);
  944. if (error)
  945. return error;
  946. error = -EROFS;
  947. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  948. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  949. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
  950. if (!rgd)
  951. goto out_inodes;
  952. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  953. error = gfs2_glock_nq(ghs); /* parent */
  954. if (error)
  955. goto out_parent;
  956. error = gfs2_glock_nq(ghs + 1); /* child */
  957. if (error)
  958. goto out_child;
  959. error = -ENOENT;
  960. if (inode->i_nlink == 0)
  961. goto out_rgrp;
  962. if (S_ISDIR(inode->i_mode)) {
  963. error = -ENOTEMPTY;
  964. if (ip->i_entries > 2 || inode->i_nlink > 2)
  965. goto out_rgrp;
  966. }
  967. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  968. if (error)
  969. goto out_rgrp;
  970. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  971. if (error)
  972. goto out_gunlock;
  973. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  974. if (error)
  975. goto out_end_trans;
  976. error = gfs2_unlink_inode(dip, dentry);
  977. out_end_trans:
  978. gfs2_trans_end(sdp);
  979. out_gunlock:
  980. gfs2_glock_dq(ghs + 2);
  981. out_rgrp:
  982. gfs2_glock_dq(ghs + 1);
  983. out_child:
  984. gfs2_glock_dq(ghs);
  985. out_parent:
  986. gfs2_holder_uninit(ghs + 2);
  987. out_inodes:
  988. gfs2_holder_uninit(ghs + 1);
  989. gfs2_holder_uninit(ghs);
  990. return error;
  991. }
  992. /**
  993. * gfs2_symlink - Create a symlink
  994. * @dir: The directory to create the symlink in
  995. * @dentry: The dentry to put the symlink in
  996. * @symname: The thing which the link points to
  997. *
  998. * Returns: errno
  999. */
  1000. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  1001. const char *symname)
  1002. {
  1003. struct gfs2_sbd *sdp = GFS2_SB(dir);
  1004. unsigned int size;
  1005. size = strlen(symname);
  1006. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  1007. return -ENAMETOOLONG;
  1008. return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0, NULL);
  1009. }
  1010. /**
  1011. * gfs2_mkdir - Make a directory
  1012. * @dir: The parent directory of the new one
  1013. * @dentry: The dentry of the new directory
  1014. * @mode: The mode of the new directory
  1015. *
  1016. * Returns: errno
  1017. */
  1018. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1019. {
  1020. struct gfs2_sbd *sdp = GFS2_SB(dir);
  1021. unsigned dsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  1022. return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0, NULL);
  1023. }
  1024. /**
  1025. * gfs2_mknod - Make a special file
  1026. * @dir: The directory in which the special file will reside
  1027. * @dentry: The dentry of the special file
  1028. * @mode: The mode of the special file
  1029. * @dev: The device specification of the special file
  1030. *
  1031. */
  1032. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  1033. dev_t dev)
  1034. {
  1035. return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0, NULL);
  1036. }
  1037. /**
  1038. * gfs2_atomic_open - Atomically open a file
  1039. * @dir: The directory
  1040. * @dentry: The proposed new entry
  1041. * @file: The proposed new struct file
  1042. * @flags: open flags
  1043. * @mode: File mode
  1044. * @opened: Flag to say whether the file has been opened or not
  1045. *
  1046. * Returns: error code or 0 for success
  1047. */
  1048. static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
  1049. struct file *file, unsigned flags,
  1050. umode_t mode, int *opened)
  1051. {
  1052. struct dentry *d;
  1053. bool excl = !!(flags & O_EXCL);
  1054. if (!d_unhashed(dentry))
  1055. goto skip_lookup;
  1056. d = __gfs2_lookup(dir, dentry, file, opened);
  1057. if (IS_ERR(d))
  1058. return PTR_ERR(d);
  1059. if (d != NULL)
  1060. dentry = d;
  1061. if (dentry->d_inode) {
  1062. if (!(*opened & FILE_OPENED))
  1063. return finish_no_open(file, d);
  1064. dput(d);
  1065. return 0;
  1066. }
  1067. BUG_ON(d != NULL);
  1068. skip_lookup:
  1069. if (!(flags & O_CREAT))
  1070. return -ENOENT;
  1071. return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl, opened);
  1072. }
  1073. /*
  1074. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  1075. * @this: move this
  1076. * @to: to here
  1077. *
  1078. * Follow @to back to the root and make sure we don't encounter @this
  1079. * Assumes we already hold the rename lock.
  1080. *
  1081. * Returns: errno
  1082. */
  1083. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1084. {
  1085. struct inode *dir = &to->i_inode;
  1086. struct super_block *sb = dir->i_sb;
  1087. struct inode *tmp;
  1088. int error = 0;
  1089. igrab(dir);
  1090. for (;;) {
  1091. if (dir == &this->i_inode) {
  1092. error = -EINVAL;
  1093. break;
  1094. }
  1095. if (dir == sb->s_root->d_inode) {
  1096. error = 0;
  1097. break;
  1098. }
  1099. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1100. if (!tmp) {
  1101. error = -ENOENT;
  1102. break;
  1103. }
  1104. if (IS_ERR(tmp)) {
  1105. error = PTR_ERR(tmp);
  1106. break;
  1107. }
  1108. iput(dir);
  1109. dir = tmp;
  1110. }
  1111. iput(dir);
  1112. return error;
  1113. }
  1114. /**
  1115. * gfs2_rename - Rename a file
  1116. * @odir: Parent directory of old file name
  1117. * @odentry: The old dentry of the file
  1118. * @ndir: Parent directory of new file name
  1119. * @ndentry: The new dentry of the file
  1120. *
  1121. * Returns: errno
  1122. */
  1123. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1124. struct inode *ndir, struct dentry *ndentry)
  1125. {
  1126. struct gfs2_inode *odip = GFS2_I(odir);
  1127. struct gfs2_inode *ndip = GFS2_I(ndir);
  1128. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  1129. struct gfs2_inode *nip = NULL;
  1130. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1131. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  1132. struct gfs2_rgrpd *nrgd;
  1133. unsigned int num_gh;
  1134. int dir_rename = 0;
  1135. struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
  1136. unsigned int x;
  1137. int error;
  1138. if (ndentry->d_inode) {
  1139. nip = GFS2_I(ndentry->d_inode);
  1140. if (ip == nip)
  1141. return 0;
  1142. }
  1143. error = gfs2_rindex_update(sdp);
  1144. if (error)
  1145. return error;
  1146. error = gfs2_rs_alloc(ndip);
  1147. if (error)
  1148. return error;
  1149. if (odip != ndip) {
  1150. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1151. 0, &r_gh);
  1152. if (error)
  1153. goto out;
  1154. if (S_ISDIR(ip->i_inode.i_mode)) {
  1155. dir_rename = 1;
  1156. /* don't move a dirctory into it's subdir */
  1157. error = gfs2_ok_to_move(ip, ndip);
  1158. if (error)
  1159. goto out_gunlock_r;
  1160. }
  1161. }
  1162. num_gh = 1;
  1163. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1164. if (odip != ndip) {
  1165. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1166. num_gh++;
  1167. }
  1168. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1169. num_gh++;
  1170. if (nip) {
  1171. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1172. num_gh++;
  1173. /* grab the resource lock for unlink flag twiddling
  1174. * this is the case of the target file already existing
  1175. * so we unlink before doing the rename
  1176. */
  1177. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
  1178. if (nrgd)
  1179. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1180. }
  1181. for (x = 0; x < num_gh; x++) {
  1182. error = gfs2_glock_nq(ghs + x);
  1183. if (error)
  1184. goto out_gunlock;
  1185. }
  1186. error = -ENOENT;
  1187. if (ip->i_inode.i_nlink == 0)
  1188. goto out_gunlock;
  1189. /* Check out the old directory */
  1190. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1191. if (error)
  1192. goto out_gunlock;
  1193. /* Check out the new directory */
  1194. if (nip) {
  1195. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1196. if (error)
  1197. goto out_gunlock;
  1198. if (nip->i_inode.i_nlink == 0) {
  1199. error = -EAGAIN;
  1200. goto out_gunlock;
  1201. }
  1202. if (S_ISDIR(nip->i_inode.i_mode)) {
  1203. if (nip->i_entries < 2) {
  1204. gfs2_consist_inode(nip);
  1205. error = -EIO;
  1206. goto out_gunlock;
  1207. }
  1208. if (nip->i_entries > 2) {
  1209. error = -ENOTEMPTY;
  1210. goto out_gunlock;
  1211. }
  1212. }
  1213. } else {
  1214. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1215. if (error)
  1216. goto out_gunlock;
  1217. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1218. switch (error) {
  1219. case -ENOENT:
  1220. error = 0;
  1221. break;
  1222. case 0:
  1223. error = -EEXIST;
  1224. default:
  1225. goto out_gunlock;
  1226. };
  1227. if (odip != ndip) {
  1228. if (!ndip->i_inode.i_nlink) {
  1229. error = -ENOENT;
  1230. goto out_gunlock;
  1231. }
  1232. if (ndip->i_entries == (u32)-1) {
  1233. error = -EFBIG;
  1234. goto out_gunlock;
  1235. }
  1236. if (S_ISDIR(ip->i_inode.i_mode) &&
  1237. ndip->i_inode.i_nlink == (u32)-1) {
  1238. error = -EMLINK;
  1239. goto out_gunlock;
  1240. }
  1241. }
  1242. }
  1243. /* Check out the dir to be renamed */
  1244. if (dir_rename) {
  1245. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1246. if (error)
  1247. goto out_gunlock;
  1248. }
  1249. if (nip == NULL) {
  1250. error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
  1251. if (error)
  1252. goto out_gunlock;
  1253. }
  1254. if (da.nr_blocks) {
  1255. struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
  1256. error = gfs2_quota_lock_check(ndip);
  1257. if (error)
  1258. goto out_gunlock;
  1259. error = gfs2_inplace_reserve(ndip, &ap);
  1260. if (error)
  1261. goto out_gunlock_q;
  1262. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
  1263. 4 * RES_LEAF + 4, 0);
  1264. if (error)
  1265. goto out_ipreserv;
  1266. } else {
  1267. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1268. 5 * RES_LEAF + 4, 0);
  1269. if (error)
  1270. goto out_gunlock;
  1271. }
  1272. /* Remove the target file, if it exists */
  1273. if (nip)
  1274. error = gfs2_unlink_inode(ndip, ndentry);
  1275. if (dir_rename) {
  1276. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1277. if (error)
  1278. goto out_end_trans;
  1279. } else {
  1280. struct buffer_head *dibh;
  1281. error = gfs2_meta_inode_buffer(ip, &dibh);
  1282. if (error)
  1283. goto out_end_trans;
  1284. ip->i_inode.i_ctime = CURRENT_TIME;
  1285. gfs2_trans_add_meta(ip->i_gl, dibh);
  1286. gfs2_dinode_out(ip, dibh->b_data);
  1287. brelse(dibh);
  1288. }
  1289. error = gfs2_dir_del(odip, odentry);
  1290. if (error)
  1291. goto out_end_trans;
  1292. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
  1293. if (error)
  1294. goto out_end_trans;
  1295. out_end_trans:
  1296. gfs2_trans_end(sdp);
  1297. out_ipreserv:
  1298. if (da.nr_blocks)
  1299. gfs2_inplace_release(ndip);
  1300. out_gunlock_q:
  1301. if (da.nr_blocks)
  1302. gfs2_quota_unlock(ndip);
  1303. out_gunlock:
  1304. gfs2_dir_no_add(&da);
  1305. while (x--) {
  1306. gfs2_glock_dq(ghs + x);
  1307. gfs2_holder_uninit(ghs + x);
  1308. }
  1309. out_gunlock_r:
  1310. if (r_gh.gh_gl)
  1311. gfs2_glock_dq_uninit(&r_gh);
  1312. out:
  1313. return error;
  1314. }
  1315. /**
  1316. * gfs2_follow_link - Follow a symbolic link
  1317. * @dentry: The dentry of the link
  1318. * @nd: Data that we pass to vfs_follow_link()
  1319. *
  1320. * This can handle symlinks of any size.
  1321. *
  1322. * Returns: 0 on success or error code
  1323. */
  1324. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  1325. {
  1326. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  1327. struct gfs2_holder i_gh;
  1328. struct buffer_head *dibh;
  1329. unsigned int size;
  1330. char *buf;
  1331. int error;
  1332. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1333. error = gfs2_glock_nq(&i_gh);
  1334. if (error) {
  1335. gfs2_holder_uninit(&i_gh);
  1336. nd_set_link(nd, ERR_PTR(error));
  1337. return NULL;
  1338. }
  1339. size = (unsigned int)i_size_read(&ip->i_inode);
  1340. if (size == 0) {
  1341. gfs2_consist_inode(ip);
  1342. buf = ERR_PTR(-EIO);
  1343. goto out;
  1344. }
  1345. error = gfs2_meta_inode_buffer(ip, &dibh);
  1346. if (error) {
  1347. buf = ERR_PTR(error);
  1348. goto out;
  1349. }
  1350. buf = kzalloc(size + 1, GFP_NOFS);
  1351. if (!buf)
  1352. buf = ERR_PTR(-ENOMEM);
  1353. else
  1354. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1355. brelse(dibh);
  1356. out:
  1357. gfs2_glock_dq_uninit(&i_gh);
  1358. nd_set_link(nd, buf);
  1359. return NULL;
  1360. }
  1361. /**
  1362. * gfs2_permission -
  1363. * @inode: The inode
  1364. * @mask: The mask to be tested
  1365. * @flags: Indicates whether this is an RCU path walk or not
  1366. *
  1367. * This may be called from the VFS directly, or from within GFS2 with the
  1368. * inode locked, so we look to see if the glock is already locked and only
  1369. * lock the glock if its not already been done.
  1370. *
  1371. * Returns: errno
  1372. */
  1373. int gfs2_permission(struct inode *inode, int mask)
  1374. {
  1375. struct gfs2_inode *ip;
  1376. struct gfs2_holder i_gh;
  1377. int error;
  1378. int unlock = 0;
  1379. ip = GFS2_I(inode);
  1380. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1381. if (mask & MAY_NOT_BLOCK)
  1382. return -ECHILD;
  1383. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1384. if (error)
  1385. return error;
  1386. unlock = 1;
  1387. }
  1388. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1389. error = -EACCES;
  1390. else
  1391. error = generic_permission(inode, mask);
  1392. if (unlock)
  1393. gfs2_glock_dq_uninit(&i_gh);
  1394. return error;
  1395. }
  1396. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1397. {
  1398. setattr_copy(inode, attr);
  1399. mark_inode_dirty(inode);
  1400. return 0;
  1401. }
  1402. /**
  1403. * gfs2_setattr_simple -
  1404. * @ip:
  1405. * @attr:
  1406. *
  1407. * Returns: errno
  1408. */
  1409. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1410. {
  1411. int error;
  1412. if (current->journal_info)
  1413. return __gfs2_setattr_simple(inode, attr);
  1414. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1415. if (error)
  1416. return error;
  1417. error = __gfs2_setattr_simple(inode, attr);
  1418. gfs2_trans_end(GFS2_SB(inode));
  1419. return error;
  1420. }
  1421. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1422. {
  1423. struct gfs2_inode *ip = GFS2_I(inode);
  1424. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1425. kuid_t ouid, nuid;
  1426. kgid_t ogid, ngid;
  1427. int error;
  1428. ouid = inode->i_uid;
  1429. ogid = inode->i_gid;
  1430. nuid = attr->ia_uid;
  1431. ngid = attr->ia_gid;
  1432. if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
  1433. ouid = nuid = NO_UID_QUOTA_CHANGE;
  1434. if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
  1435. ogid = ngid = NO_GID_QUOTA_CHANGE;
  1436. error = get_write_access(inode);
  1437. if (error)
  1438. return error;
  1439. error = gfs2_rs_alloc(ip);
  1440. if (error)
  1441. goto out;
  1442. error = gfs2_rindex_update(sdp);
  1443. if (error)
  1444. goto out;
  1445. error = gfs2_quota_lock(ip, nuid, ngid);
  1446. if (error)
  1447. goto out;
  1448. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1449. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1450. error = gfs2_quota_check(ip, nuid, ngid);
  1451. if (error)
  1452. goto out_gunlock_q;
  1453. }
  1454. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1455. if (error)
  1456. goto out_gunlock_q;
  1457. error = gfs2_setattr_simple(inode, attr);
  1458. if (error)
  1459. goto out_end_trans;
  1460. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1461. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1462. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1463. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1464. gfs2_quota_change(ip, blocks, nuid, ngid);
  1465. }
  1466. out_end_trans:
  1467. gfs2_trans_end(sdp);
  1468. out_gunlock_q:
  1469. gfs2_quota_unlock(ip);
  1470. out:
  1471. put_write_access(inode);
  1472. return error;
  1473. }
  1474. /**
  1475. * gfs2_setattr - Change attributes on an inode
  1476. * @dentry: The dentry which is changing
  1477. * @attr: The structure describing the change
  1478. *
  1479. * The VFS layer wants to change one or more of an inodes attributes. Write
  1480. * that change out to disk.
  1481. *
  1482. * Returns: errno
  1483. */
  1484. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1485. {
  1486. struct inode *inode = dentry->d_inode;
  1487. struct gfs2_inode *ip = GFS2_I(inode);
  1488. struct gfs2_holder i_gh;
  1489. int error;
  1490. error = gfs2_rs_alloc(ip);
  1491. if (error)
  1492. return error;
  1493. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1494. if (error)
  1495. return error;
  1496. error = -EPERM;
  1497. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1498. goto out;
  1499. error = inode_change_ok(inode, attr);
  1500. if (error)
  1501. goto out;
  1502. if (attr->ia_valid & ATTR_SIZE)
  1503. error = gfs2_setattr_size(inode, attr->ia_size);
  1504. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1505. error = setattr_chown(inode, attr);
  1506. else {
  1507. error = gfs2_setattr_simple(inode, attr);
  1508. if (!error && attr->ia_valid & ATTR_MODE)
  1509. error = posix_acl_chmod(inode, inode->i_mode);
  1510. }
  1511. out:
  1512. if (!error)
  1513. mark_inode_dirty(inode);
  1514. gfs2_glock_dq_uninit(&i_gh);
  1515. return error;
  1516. }
  1517. /**
  1518. * gfs2_getattr - Read out an inode's attributes
  1519. * @mnt: The vfsmount the inode is being accessed from
  1520. * @dentry: The dentry to stat
  1521. * @stat: The inode's stats
  1522. *
  1523. * This may be called from the VFS directly, or from within GFS2 with the
  1524. * inode locked, so we look to see if the glock is already locked and only
  1525. * lock the glock if its not already been done. Note that its the NFS
  1526. * readdirplus operation which causes this to be called (from filldir)
  1527. * with the glock already held.
  1528. *
  1529. * Returns: errno
  1530. */
  1531. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1532. struct kstat *stat)
  1533. {
  1534. struct inode *inode = dentry->d_inode;
  1535. struct gfs2_inode *ip = GFS2_I(inode);
  1536. struct gfs2_holder gh;
  1537. int error;
  1538. int unlock = 0;
  1539. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1540. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1541. if (error)
  1542. return error;
  1543. unlock = 1;
  1544. }
  1545. generic_fillattr(inode, stat);
  1546. if (unlock)
  1547. gfs2_glock_dq_uninit(&gh);
  1548. return 0;
  1549. }
  1550. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1551. const void *data, size_t size, int flags)
  1552. {
  1553. struct inode *inode = dentry->d_inode;
  1554. struct gfs2_inode *ip = GFS2_I(inode);
  1555. struct gfs2_holder gh;
  1556. int ret;
  1557. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1558. ret = gfs2_glock_nq(&gh);
  1559. if (ret == 0) {
  1560. ret = gfs2_rs_alloc(ip);
  1561. if (ret == 0)
  1562. ret = generic_setxattr(dentry, name, data, size, flags);
  1563. gfs2_glock_dq(&gh);
  1564. }
  1565. gfs2_holder_uninit(&gh);
  1566. return ret;
  1567. }
  1568. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1569. void *data, size_t size)
  1570. {
  1571. struct inode *inode = dentry->d_inode;
  1572. struct gfs2_inode *ip = GFS2_I(inode);
  1573. struct gfs2_holder gh;
  1574. int ret;
  1575. /* For selinux during lookup */
  1576. if (gfs2_glock_is_locked_by_me(ip->i_gl))
  1577. return generic_getxattr(dentry, name, data, size);
  1578. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1579. ret = gfs2_glock_nq(&gh);
  1580. if (ret == 0) {
  1581. ret = generic_getxattr(dentry, name, data, size);
  1582. gfs2_glock_dq(&gh);
  1583. }
  1584. gfs2_holder_uninit(&gh);
  1585. return ret;
  1586. }
  1587. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1588. {
  1589. struct inode *inode = dentry->d_inode;
  1590. struct gfs2_inode *ip = GFS2_I(inode);
  1591. struct gfs2_holder gh;
  1592. int ret;
  1593. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1594. ret = gfs2_glock_nq(&gh);
  1595. if (ret == 0) {
  1596. ret = gfs2_rs_alloc(ip);
  1597. if (ret == 0)
  1598. ret = generic_removexattr(dentry, name);
  1599. gfs2_glock_dq(&gh);
  1600. }
  1601. gfs2_holder_uninit(&gh);
  1602. return ret;
  1603. }
  1604. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1605. u64 start, u64 len)
  1606. {
  1607. struct gfs2_inode *ip = GFS2_I(inode);
  1608. struct gfs2_holder gh;
  1609. int ret;
  1610. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1611. if (ret)
  1612. return ret;
  1613. mutex_lock(&inode->i_mutex);
  1614. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1615. if (ret)
  1616. goto out;
  1617. if (gfs2_is_stuffed(ip)) {
  1618. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1619. u64 size = i_size_read(inode);
  1620. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1621. FIEMAP_EXTENT_DATA_INLINE;
  1622. phys += sizeof(struct gfs2_dinode);
  1623. phys += start;
  1624. if (start + len > size)
  1625. len = size - start;
  1626. if (start < size)
  1627. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1628. len, flags);
  1629. if (ret == 1)
  1630. ret = 0;
  1631. } else {
  1632. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1633. gfs2_block_map);
  1634. }
  1635. gfs2_glock_dq_uninit(&gh);
  1636. out:
  1637. mutex_unlock(&inode->i_mutex);
  1638. return ret;
  1639. }
  1640. const struct inode_operations gfs2_file_iops = {
  1641. .permission = gfs2_permission,
  1642. .setattr = gfs2_setattr,
  1643. .getattr = gfs2_getattr,
  1644. .setxattr = gfs2_setxattr,
  1645. .getxattr = gfs2_getxattr,
  1646. .listxattr = gfs2_listxattr,
  1647. .removexattr = gfs2_removexattr,
  1648. .fiemap = gfs2_fiemap,
  1649. .get_acl = gfs2_get_acl,
  1650. .set_acl = gfs2_set_acl,
  1651. };
  1652. const struct inode_operations gfs2_dir_iops = {
  1653. .create = gfs2_create,
  1654. .lookup = gfs2_lookup,
  1655. .link = gfs2_link,
  1656. .unlink = gfs2_unlink,
  1657. .symlink = gfs2_symlink,
  1658. .mkdir = gfs2_mkdir,
  1659. .rmdir = gfs2_unlink,
  1660. .mknod = gfs2_mknod,
  1661. .rename = gfs2_rename,
  1662. .permission = gfs2_permission,
  1663. .setattr = gfs2_setattr,
  1664. .getattr = gfs2_getattr,
  1665. .setxattr = gfs2_setxattr,
  1666. .getxattr = gfs2_getxattr,
  1667. .listxattr = gfs2_listxattr,
  1668. .removexattr = gfs2_removexattr,
  1669. .fiemap = gfs2_fiemap,
  1670. .get_acl = gfs2_get_acl,
  1671. .set_acl = gfs2_set_acl,
  1672. .atomic_open = gfs2_atomic_open,
  1673. };
  1674. const struct inode_operations gfs2_symlink_iops = {
  1675. .readlink = generic_readlink,
  1676. .follow_link = gfs2_follow_link,
  1677. .put_link = kfree_put_link,
  1678. .permission = gfs2_permission,
  1679. .setattr = gfs2_setattr,
  1680. .getattr = gfs2_getattr,
  1681. .setxattr = gfs2_setxattr,
  1682. .getxattr = gfs2_getxattr,
  1683. .listxattr = gfs2_listxattr,
  1684. .removexattr = gfs2_removexattr,
  1685. .fiemap = gfs2_fiemap,
  1686. };