inode.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  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. static int iget_test(struct inode *inode, void *opaque)
  38. {
  39. u64 no_addr = *(u64 *)opaque;
  40. return GFS2_I(inode)->i_no_addr == no_addr;
  41. }
  42. static int iget_set(struct inode *inode, void *opaque)
  43. {
  44. u64 no_addr = *(u64 *)opaque;
  45. GFS2_I(inode)->i_no_addr = no_addr;
  46. inode->i_ino = no_addr;
  47. return 0;
  48. }
  49. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
  50. {
  51. struct inode *inode;
  52. repeat:
  53. inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
  54. if (!inode)
  55. return inode;
  56. if (is_bad_inode(inode)) {
  57. iput(inode);
  58. goto repeat;
  59. }
  60. return inode;
  61. }
  62. /**
  63. * gfs2_set_iop - Sets inode operations
  64. * @inode: The inode with correct i_mode filled in
  65. *
  66. * GFS2 lookup code fills in vfs inode contents based on info obtained
  67. * from directory entry inside gfs2_inode_lookup().
  68. */
  69. static void gfs2_set_iop(struct inode *inode)
  70. {
  71. struct gfs2_sbd *sdp = GFS2_SB(inode);
  72. umode_t mode = inode->i_mode;
  73. if (S_ISREG(mode)) {
  74. inode->i_op = &gfs2_file_iops;
  75. if (gfs2_localflocks(sdp))
  76. inode->i_fop = &gfs2_file_fops_nolock;
  77. else
  78. inode->i_fop = &gfs2_file_fops;
  79. } else if (S_ISDIR(mode)) {
  80. inode->i_op = &gfs2_dir_iops;
  81. if (gfs2_localflocks(sdp))
  82. inode->i_fop = &gfs2_dir_fops_nolock;
  83. else
  84. inode->i_fop = &gfs2_dir_fops;
  85. } else if (S_ISLNK(mode)) {
  86. inode->i_op = &gfs2_symlink_iops;
  87. } else {
  88. inode->i_op = &gfs2_file_iops;
  89. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  90. }
  91. }
  92. /**
  93. * gfs2_inode_lookup - Lookup an inode
  94. * @sb: The super block
  95. * @type: The type of the inode
  96. * @no_addr: The inode number
  97. * @no_formal_ino: The inode generation number
  98. * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED;
  99. * GFS2_BLKST_FREE do indicate not to verify)
  100. *
  101. * If @type is DT_UNKNOWN, the inode type is fetched from disk.
  102. *
  103. * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a
  104. * placeholder because it doesn't otherwise make sense), the on-disk block type
  105. * is verified to be @blktype.
  106. *
  107. * Returns: A VFS inode, or an error
  108. */
  109. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  110. u64 no_addr, u64 no_formal_ino,
  111. unsigned int blktype)
  112. {
  113. struct inode *inode;
  114. struct gfs2_inode *ip;
  115. struct gfs2_glock *io_gl = NULL;
  116. struct gfs2_holder i_gh;
  117. int error;
  118. gfs2_holder_mark_uninitialized(&i_gh);
  119. inode = gfs2_iget(sb, no_addr);
  120. if (!inode)
  121. return ERR_PTR(-ENOMEM);
  122. ip = GFS2_I(inode);
  123. if (inode->i_state & I_NEW) {
  124. struct gfs2_sbd *sdp = GFS2_SB(inode);
  125. ip->i_no_formal_ino = no_formal_ino;
  126. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  127. if (unlikely(error))
  128. goto fail;
  129. ip->i_gl->gl_object = ip;
  130. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  131. if (unlikely(error))
  132. goto fail_put;
  133. if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
  134. /*
  135. * The GL_SKIP flag indicates to skip reading the inode
  136. * block. We read the inode with gfs2_inode_refresh
  137. * after possibly checking the block type.
  138. */
  139. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
  140. GL_SKIP, &i_gh);
  141. if (error)
  142. goto fail_put;
  143. if (blktype != GFS2_BLKST_FREE) {
  144. error = gfs2_check_blk_type(sdp, no_addr,
  145. blktype);
  146. if (error)
  147. goto fail_put;
  148. }
  149. }
  150. set_bit(GIF_INVALID, &ip->i_flags);
  151. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  152. if (unlikely(error))
  153. goto fail_put;
  154. ip->i_iopen_gh.gh_gl->gl_object = ip;
  155. gfs2_glock_put(io_gl);
  156. io_gl = NULL;
  157. if (type == DT_UNKNOWN) {
  158. /* Inode glock must be locked already */
  159. error = gfs2_inode_refresh(GFS2_I(inode));
  160. if (error)
  161. goto fail_refresh;
  162. } else {
  163. inode->i_mode = DT2IF(type);
  164. }
  165. gfs2_set_iop(inode);
  166. inode->i_atime.tv_sec = 0;
  167. inode->i_atime.tv_nsec = 0;
  168. unlock_new_inode(inode);
  169. }
  170. if (gfs2_holder_initialized(&i_gh))
  171. gfs2_glock_dq_uninit(&i_gh);
  172. return inode;
  173. fail_refresh:
  174. ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
  175. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  176. gfs2_glock_dq_wait(&ip->i_iopen_gh);
  177. gfs2_holder_uninit(&ip->i_iopen_gh);
  178. fail_put:
  179. if (io_gl)
  180. gfs2_glock_put(io_gl);
  181. if (gfs2_holder_initialized(&i_gh))
  182. gfs2_glock_dq_uninit(&i_gh);
  183. ip->i_gl->gl_object = NULL;
  184. fail:
  185. iget_failed(inode);
  186. return ERR_PTR(error);
  187. }
  188. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  189. u64 *no_formal_ino, unsigned int blktype)
  190. {
  191. struct super_block *sb = sdp->sd_vfs;
  192. struct inode *inode;
  193. int error;
  194. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, blktype);
  195. if (IS_ERR(inode))
  196. return inode;
  197. /* Two extra checks for NFS only */
  198. if (no_formal_ino) {
  199. error = -ESTALE;
  200. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  201. goto fail_iput;
  202. error = -EIO;
  203. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  204. goto fail_iput;
  205. }
  206. return inode;
  207. fail_iput:
  208. iput(inode);
  209. return ERR_PTR(error);
  210. }
  211. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  212. {
  213. struct qstr qstr;
  214. struct inode *inode;
  215. gfs2_str2qstr(&qstr, name);
  216. inode = gfs2_lookupi(dip, &qstr, 1);
  217. /* gfs2_lookupi has inconsistent callers: vfs
  218. * related routines expect NULL for no entry found,
  219. * gfs2_lookup_simple callers expect ENOENT
  220. * and do not check for NULL.
  221. */
  222. if (inode == NULL)
  223. return ERR_PTR(-ENOENT);
  224. else
  225. return inode;
  226. }
  227. /**
  228. * gfs2_lookupi - Look up a filename in a directory and return its inode
  229. * @d_gh: An initialized holder for the directory glock
  230. * @name: The name of the inode to look for
  231. * @is_root: If 1, ignore the caller's permissions
  232. * @i_gh: An uninitialized holder for the new inode glock
  233. *
  234. * This can be called via the VFS filldir function when NFS is doing
  235. * a readdirplus and the inode which its intending to stat isn't
  236. * already in cache. In this case we must not take the directory glock
  237. * again, since the readdir call will have already taken that lock.
  238. *
  239. * Returns: errno
  240. */
  241. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  242. int is_root)
  243. {
  244. struct super_block *sb = dir->i_sb;
  245. struct gfs2_inode *dip = GFS2_I(dir);
  246. struct gfs2_holder d_gh;
  247. int error = 0;
  248. struct inode *inode = NULL;
  249. gfs2_holder_mark_uninitialized(&d_gh);
  250. if (!name->len || name->len > GFS2_FNAMESIZE)
  251. return ERR_PTR(-ENAMETOOLONG);
  252. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  253. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  254. dir == d_inode(sb->s_root))) {
  255. igrab(dir);
  256. return dir;
  257. }
  258. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  259. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  260. if (error)
  261. return ERR_PTR(error);
  262. }
  263. if (!is_root) {
  264. error = gfs2_permission(dir, MAY_EXEC);
  265. if (error)
  266. goto out;
  267. }
  268. inode = gfs2_dir_search(dir, name, false);
  269. if (IS_ERR(inode))
  270. error = PTR_ERR(inode);
  271. out:
  272. if (gfs2_holder_initialized(&d_gh))
  273. gfs2_glock_dq_uninit(&d_gh);
  274. if (error == -ENOENT)
  275. return NULL;
  276. return inode ? inode : ERR_PTR(error);
  277. }
  278. /**
  279. * create_ok - OK to create a new on-disk inode here?
  280. * @dip: Directory in which dinode is to be created
  281. * @name: Name of new dinode
  282. * @mode:
  283. *
  284. * Returns: errno
  285. */
  286. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  287. umode_t mode)
  288. {
  289. int error;
  290. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  291. if (error)
  292. return error;
  293. /* Don't create entries in an unlinked directory */
  294. if (!dip->i_inode.i_nlink)
  295. return -ENOENT;
  296. if (dip->i_entries == (u32)-1)
  297. return -EFBIG;
  298. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  299. return -EMLINK;
  300. return 0;
  301. }
  302. static void munge_mode_uid_gid(const struct gfs2_inode *dip,
  303. struct inode *inode)
  304. {
  305. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  306. (dip->i_inode.i_mode & S_ISUID) &&
  307. !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
  308. if (S_ISDIR(inode->i_mode))
  309. inode->i_mode |= S_ISUID;
  310. else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
  311. inode->i_mode &= ~07111;
  312. inode->i_uid = dip->i_inode.i_uid;
  313. } else
  314. inode->i_uid = current_fsuid();
  315. if (dip->i_inode.i_mode & S_ISGID) {
  316. if (S_ISDIR(inode->i_mode))
  317. inode->i_mode |= S_ISGID;
  318. inode->i_gid = dip->i_inode.i_gid;
  319. } else
  320. inode->i_gid = current_fsgid();
  321. }
  322. static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
  323. {
  324. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  325. struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
  326. int error;
  327. error = gfs2_quota_lock_check(ip, &ap);
  328. if (error)
  329. goto out;
  330. error = gfs2_inplace_reserve(ip, &ap);
  331. if (error)
  332. goto out_quota;
  333. error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
  334. if (error)
  335. goto out_ipreserv;
  336. error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
  337. ip->i_no_formal_ino = ip->i_generation;
  338. ip->i_inode.i_ino = ip->i_no_addr;
  339. ip->i_goal = ip->i_no_addr;
  340. gfs2_trans_end(sdp);
  341. out_ipreserv:
  342. gfs2_inplace_release(ip);
  343. out_quota:
  344. gfs2_quota_unlock(ip);
  345. out:
  346. return error;
  347. }
  348. static void gfs2_init_dir(struct buffer_head *dibh,
  349. const struct gfs2_inode *parent)
  350. {
  351. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  352. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  353. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  354. dent->de_inum = di->di_num; /* already GFS2 endian */
  355. dent->de_type = cpu_to_be16(DT_DIR);
  356. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  357. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  358. gfs2_inum_out(parent, dent);
  359. dent->de_type = cpu_to_be16(DT_DIR);
  360. }
  361. /**
  362. * gfs2_init_xattr - Initialise an xattr block for a new inode
  363. * @ip: The inode in question
  364. *
  365. * This sets up an empty xattr block for a new inode, ready to
  366. * take any ACLs, LSM xattrs, etc.
  367. */
  368. static void gfs2_init_xattr(struct gfs2_inode *ip)
  369. {
  370. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  371. struct buffer_head *bh;
  372. struct gfs2_ea_header *ea;
  373. bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
  374. gfs2_trans_add_meta(ip->i_gl, bh);
  375. gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
  376. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  377. ea = GFS2_EA_BH2FIRST(bh);
  378. ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
  379. ea->ea_type = GFS2_EATYPE_UNUSED;
  380. ea->ea_flags = GFS2_EAFLAG_LAST;
  381. brelse(bh);
  382. }
  383. /**
  384. * init_dinode - Fill in a new dinode structure
  385. * @dip: The directory this inode is being created in
  386. * @ip: The inode
  387. * @symname: The symlink destination (if a symlink)
  388. * @bhp: The buffer head (returned to caller)
  389. *
  390. */
  391. static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  392. const char *symname)
  393. {
  394. struct gfs2_dinode *di;
  395. struct buffer_head *dibh;
  396. dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
  397. gfs2_trans_add_meta(ip->i_gl, dibh);
  398. di = (struct gfs2_dinode *)dibh->b_data;
  399. gfs2_dinode_out(ip, di);
  400. di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
  401. di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
  402. di->__pad1 = 0;
  403. di->__pad2 = 0;
  404. di->__pad3 = 0;
  405. memset(&di->__pad4, 0, sizeof(di->__pad4));
  406. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  407. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  408. switch(ip->i_inode.i_mode & S_IFMT) {
  409. case S_IFDIR:
  410. gfs2_init_dir(dibh, dip);
  411. break;
  412. case S_IFLNK:
  413. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
  414. break;
  415. }
  416. set_buffer_uptodate(dibh);
  417. brelse(dibh);
  418. }
  419. /**
  420. * gfs2_trans_da_blocks - Calculate number of blocks to link inode
  421. * @dip: The directory we are linking into
  422. * @da: The dir add information
  423. * @nr_inodes: The number of inodes involved
  424. *
  425. * This calculate the number of blocks we need to reserve in a
  426. * transaction to link @nr_inodes into a directory. In most cases
  427. * @nr_inodes will be 2 (the directory plus the inode being linked in)
  428. * but in case of rename, 4 may be required.
  429. *
  430. * Returns: Number of blocks
  431. */
  432. static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
  433. const struct gfs2_diradd *da,
  434. unsigned nr_inodes)
  435. {
  436. return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
  437. (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
  438. }
  439. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  440. struct gfs2_inode *ip, struct gfs2_diradd *da)
  441. {
  442. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  443. struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
  444. int error;
  445. if (da->nr_blocks) {
  446. error = gfs2_quota_lock_check(dip, &ap);
  447. if (error)
  448. goto fail_quota_locks;
  449. error = gfs2_inplace_reserve(dip, &ap);
  450. if (error)
  451. goto fail_quota_locks;
  452. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
  453. if (error)
  454. goto fail_ipreserv;
  455. } else {
  456. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  457. if (error)
  458. goto fail_quota_locks;
  459. }
  460. error = gfs2_dir_add(&dip->i_inode, name, ip, da);
  461. gfs2_trans_end(sdp);
  462. fail_ipreserv:
  463. gfs2_inplace_release(dip);
  464. fail_quota_locks:
  465. gfs2_quota_unlock(dip);
  466. return error;
  467. }
  468. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  469. void *fs_info)
  470. {
  471. const struct xattr *xattr;
  472. int err = 0;
  473. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  474. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  475. xattr->value_len, 0,
  476. GFS2_EATYPE_SECURITY);
  477. if (err < 0)
  478. break;
  479. }
  480. return err;
  481. }
  482. /**
  483. * gfs2_create_inode - Create a new inode
  484. * @dir: The parent directory
  485. * @dentry: The new dentry
  486. * @file: If non-NULL, the file which is being opened
  487. * @mode: The permissions on the new inode
  488. * @dev: For device nodes, this is the device number
  489. * @symname: For symlinks, this is the link destination
  490. * @size: The initial size of the inode (ignored for directories)
  491. *
  492. * Returns: 0 on success, or error code
  493. */
  494. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  495. struct file *file,
  496. umode_t mode, dev_t dev, const char *symname,
  497. unsigned int size, int excl, int *opened)
  498. {
  499. const struct qstr *name = &dentry->d_name;
  500. struct posix_acl *default_acl, *acl;
  501. struct gfs2_holder ghs[2];
  502. struct inode *inode = NULL;
  503. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  504. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  505. struct gfs2_glock *io_gl = NULL;
  506. int error, free_vfs_inode = 1;
  507. u32 aflags = 0;
  508. unsigned blocks = 1;
  509. struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
  510. if (!name->len || name->len > GFS2_FNAMESIZE)
  511. return -ENAMETOOLONG;
  512. error = gfs2_rsqa_alloc(dip);
  513. if (error)
  514. return error;
  515. error = gfs2_rindex_update(sdp);
  516. if (error)
  517. return error;
  518. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  519. if (error)
  520. goto fail;
  521. error = create_ok(dip, name, mode);
  522. if (error)
  523. goto fail_gunlock;
  524. inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
  525. error = PTR_ERR(inode);
  526. if (!IS_ERR(inode)) {
  527. if (S_ISDIR(inode->i_mode)) {
  528. iput(inode);
  529. inode = ERR_PTR(-EISDIR);
  530. goto fail_gunlock;
  531. }
  532. d_instantiate(dentry, inode);
  533. error = 0;
  534. if (file) {
  535. if (S_ISREG(inode->i_mode))
  536. error = finish_open(file, dentry, gfs2_open_common, opened);
  537. else
  538. error = finish_no_open(file, NULL);
  539. }
  540. gfs2_glock_dq_uninit(ghs);
  541. return error;
  542. } else if (error != -ENOENT) {
  543. goto fail_gunlock;
  544. }
  545. error = gfs2_diradd_alloc_required(dir, name, &da);
  546. if (error < 0)
  547. goto fail_gunlock;
  548. inode = new_inode(sdp->sd_vfs);
  549. error = -ENOMEM;
  550. if (!inode)
  551. goto fail_gunlock;
  552. error = posix_acl_create(dir, &mode, &default_acl, &acl);
  553. if (error)
  554. goto fail_gunlock;
  555. ip = GFS2_I(inode);
  556. error = gfs2_rsqa_alloc(ip);
  557. if (error)
  558. goto fail_free_acls;
  559. inode->i_mode = mode;
  560. set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
  561. inode->i_rdev = dev;
  562. inode->i_size = size;
  563. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  564. gfs2_set_inode_blocks(inode, 1);
  565. munge_mode_uid_gid(dip, inode);
  566. check_and_update_goal(dip);
  567. ip->i_goal = dip->i_goal;
  568. ip->i_diskflags = 0;
  569. ip->i_eattr = 0;
  570. ip->i_height = 0;
  571. ip->i_depth = 0;
  572. ip->i_entries = 0;
  573. switch(mode & S_IFMT) {
  574. case S_IFREG:
  575. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  576. gfs2_tune_get(sdp, gt_new_files_jdata))
  577. ip->i_diskflags |= GFS2_DIF_JDATA;
  578. gfs2_set_aops(inode);
  579. break;
  580. case S_IFDIR:
  581. ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
  582. ip->i_diskflags |= GFS2_DIF_JDATA;
  583. ip->i_entries = 2;
  584. break;
  585. }
  586. /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
  587. if (dip->i_diskflags & GFS2_DIF_SYSTEM)
  588. ip->i_diskflags |= GFS2_DIF_SYSTEM;
  589. gfs2_set_inode_flags(inode);
  590. if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
  591. (dip->i_diskflags & GFS2_DIF_TOPDIR))
  592. aflags |= GFS2_AF_ORLOV;
  593. if (default_acl || acl)
  594. blocks++;
  595. error = alloc_dinode(ip, aflags, &blocks);
  596. if (error)
  597. goto fail_free_inode;
  598. gfs2_set_inode_blocks(inode, blocks);
  599. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  600. if (error)
  601. goto fail_free_inode;
  602. ip->i_gl->gl_object = ip;
  603. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  604. if (error)
  605. goto fail_free_inode;
  606. error = gfs2_trans_begin(sdp, blocks, 0);
  607. if (error)
  608. goto fail_gunlock2;
  609. if (blocks > 1) {
  610. ip->i_eattr = ip->i_no_addr + 1;
  611. gfs2_init_xattr(ip);
  612. }
  613. init_dinode(dip, ip, symname);
  614. gfs2_trans_end(sdp);
  615. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  616. if (error)
  617. goto fail_gunlock2;
  618. BUG_ON(test_and_set_bit(GLF_INODE_CREATING, &io_gl->gl_flags));
  619. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  620. if (error)
  621. goto fail_gunlock2;
  622. ip->i_iopen_gh.gh_gl->gl_object = ip;
  623. gfs2_glock_put(io_gl);
  624. gfs2_set_iop(inode);
  625. insert_inode_hash(inode);
  626. free_vfs_inode = 0; /* After this point, the inode is no longer
  627. considered free. Any failures need to undo
  628. the gfs2 structures. */
  629. if (default_acl) {
  630. error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  631. posix_acl_release(default_acl);
  632. }
  633. if (acl) {
  634. if (!error)
  635. error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
  636. posix_acl_release(acl);
  637. }
  638. if (error)
  639. goto fail_gunlock3;
  640. error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
  641. &gfs2_initxattrs, NULL);
  642. if (error)
  643. goto fail_gunlock3;
  644. error = link_dinode(dip, name, ip, &da);
  645. if (error)
  646. goto fail_gunlock3;
  647. mark_inode_dirty(inode);
  648. d_instantiate(dentry, inode);
  649. if (file) {
  650. *opened |= FILE_CREATED;
  651. error = finish_open(file, dentry, gfs2_open_common, opened);
  652. }
  653. gfs2_glock_dq_uninit(ghs);
  654. gfs2_glock_dq_uninit(ghs + 1);
  655. clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
  656. return error;
  657. fail_gunlock3:
  658. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  659. gfs2_glock_put(io_gl);
  660. fail_gunlock2:
  661. if (io_gl)
  662. clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
  663. gfs2_glock_dq_uninit(ghs + 1);
  664. fail_free_inode:
  665. if (ip->i_gl)
  666. gfs2_glock_put(ip->i_gl);
  667. gfs2_rsqa_delete(ip, NULL);
  668. fail_free_acls:
  669. if (default_acl)
  670. posix_acl_release(default_acl);
  671. if (acl)
  672. posix_acl_release(acl);
  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 = d_inode(old_dentry);
  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_rsqa_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, &ap);
  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(&ip->i_inode);
  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 = d_inode(dentry);
  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(inode);
  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 = d_inode(dentry);
  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_in_lookup(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 (d_really_is_positive(dentry)) {
  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 == d_inode(sb->s_root)) {
  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. * update_moved_ino - Update an inode that's being moved
  1116. * @ip: The inode being moved
  1117. * @ndip: The parent directory of the new filename
  1118. * @dir_rename: True of ip is a directory
  1119. *
  1120. * Returns: errno
  1121. */
  1122. static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
  1123. int dir_rename)
  1124. {
  1125. int error;
  1126. struct buffer_head *dibh;
  1127. if (dir_rename)
  1128. return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1129. error = gfs2_meta_inode_buffer(ip, &dibh);
  1130. if (error)
  1131. return error;
  1132. ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1133. gfs2_trans_add_meta(ip->i_gl, dibh);
  1134. gfs2_dinode_out(ip, dibh->b_data);
  1135. brelse(dibh);
  1136. return 0;
  1137. }
  1138. /**
  1139. * gfs2_rename - Rename a file
  1140. * @odir: Parent directory of old file name
  1141. * @odentry: The old dentry of the file
  1142. * @ndir: Parent directory of new file name
  1143. * @ndentry: The new dentry of the file
  1144. *
  1145. * Returns: errno
  1146. */
  1147. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1148. struct inode *ndir, struct dentry *ndentry)
  1149. {
  1150. struct gfs2_inode *odip = GFS2_I(odir);
  1151. struct gfs2_inode *ndip = GFS2_I(ndir);
  1152. struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
  1153. struct gfs2_inode *nip = NULL;
  1154. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1155. struct gfs2_holder ghs[5], r_gh;
  1156. struct gfs2_rgrpd *nrgd;
  1157. unsigned int num_gh;
  1158. int dir_rename = 0;
  1159. struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
  1160. unsigned int x;
  1161. int error;
  1162. gfs2_holder_mark_uninitialized(&r_gh);
  1163. if (d_really_is_positive(ndentry)) {
  1164. nip = GFS2_I(d_inode(ndentry));
  1165. if (ip == nip)
  1166. return 0;
  1167. }
  1168. error = gfs2_rindex_update(sdp);
  1169. if (error)
  1170. return error;
  1171. error = gfs2_rsqa_alloc(ndip);
  1172. if (error)
  1173. return error;
  1174. if (odip != ndip) {
  1175. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1176. 0, &r_gh);
  1177. if (error)
  1178. goto out;
  1179. if (S_ISDIR(ip->i_inode.i_mode)) {
  1180. dir_rename = 1;
  1181. /* don't move a directory into its subdir */
  1182. error = gfs2_ok_to_move(ip, ndip);
  1183. if (error)
  1184. goto out_gunlock_r;
  1185. }
  1186. }
  1187. num_gh = 1;
  1188. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1189. if (odip != ndip) {
  1190. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1191. num_gh++;
  1192. }
  1193. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1194. num_gh++;
  1195. if (nip) {
  1196. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1197. num_gh++;
  1198. /* grab the resource lock for unlink flag twiddling
  1199. * this is the case of the target file already existing
  1200. * so we unlink before doing the rename
  1201. */
  1202. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
  1203. if (nrgd)
  1204. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1205. }
  1206. for (x = 0; x < num_gh; x++) {
  1207. error = gfs2_glock_nq(ghs + x);
  1208. if (error)
  1209. goto out_gunlock;
  1210. }
  1211. error = -ENOENT;
  1212. if (ip->i_inode.i_nlink == 0)
  1213. goto out_gunlock;
  1214. /* Check out the old directory */
  1215. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1216. if (error)
  1217. goto out_gunlock;
  1218. /* Check out the new directory */
  1219. if (nip) {
  1220. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1221. if (error)
  1222. goto out_gunlock;
  1223. if (nip->i_inode.i_nlink == 0) {
  1224. error = -EAGAIN;
  1225. goto out_gunlock;
  1226. }
  1227. if (S_ISDIR(nip->i_inode.i_mode)) {
  1228. if (nip->i_entries < 2) {
  1229. gfs2_consist_inode(nip);
  1230. error = -EIO;
  1231. goto out_gunlock;
  1232. }
  1233. if (nip->i_entries > 2) {
  1234. error = -ENOTEMPTY;
  1235. goto out_gunlock;
  1236. }
  1237. }
  1238. } else {
  1239. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1240. if (error)
  1241. goto out_gunlock;
  1242. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1243. switch (error) {
  1244. case -ENOENT:
  1245. error = 0;
  1246. break;
  1247. case 0:
  1248. error = -EEXIST;
  1249. default:
  1250. goto out_gunlock;
  1251. };
  1252. if (odip != ndip) {
  1253. if (!ndip->i_inode.i_nlink) {
  1254. error = -ENOENT;
  1255. goto out_gunlock;
  1256. }
  1257. if (ndip->i_entries == (u32)-1) {
  1258. error = -EFBIG;
  1259. goto out_gunlock;
  1260. }
  1261. if (S_ISDIR(ip->i_inode.i_mode) &&
  1262. ndip->i_inode.i_nlink == (u32)-1) {
  1263. error = -EMLINK;
  1264. goto out_gunlock;
  1265. }
  1266. }
  1267. }
  1268. /* Check out the dir to be renamed */
  1269. if (dir_rename) {
  1270. error = gfs2_permission(d_inode(odentry), MAY_WRITE);
  1271. if (error)
  1272. goto out_gunlock;
  1273. }
  1274. if (nip == NULL) {
  1275. error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
  1276. if (error)
  1277. goto out_gunlock;
  1278. }
  1279. if (da.nr_blocks) {
  1280. struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
  1281. error = gfs2_quota_lock_check(ndip, &ap);
  1282. if (error)
  1283. goto out_gunlock;
  1284. error = gfs2_inplace_reserve(ndip, &ap);
  1285. if (error)
  1286. goto out_gunlock_q;
  1287. error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
  1288. 4 * RES_LEAF + 4, 0);
  1289. if (error)
  1290. goto out_ipreserv;
  1291. } else {
  1292. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1293. 5 * RES_LEAF + 4, 0);
  1294. if (error)
  1295. goto out_gunlock;
  1296. }
  1297. /* Remove the target file, if it exists */
  1298. if (nip)
  1299. error = gfs2_unlink_inode(ndip, ndentry);
  1300. error = update_moved_ino(ip, ndip, dir_rename);
  1301. if (error)
  1302. goto out_end_trans;
  1303. error = gfs2_dir_del(odip, odentry);
  1304. if (error)
  1305. goto out_end_trans;
  1306. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
  1307. if (error)
  1308. goto out_end_trans;
  1309. out_end_trans:
  1310. gfs2_trans_end(sdp);
  1311. out_ipreserv:
  1312. if (da.nr_blocks)
  1313. gfs2_inplace_release(ndip);
  1314. out_gunlock_q:
  1315. if (da.nr_blocks)
  1316. gfs2_quota_unlock(ndip);
  1317. out_gunlock:
  1318. gfs2_dir_no_add(&da);
  1319. while (x--) {
  1320. gfs2_glock_dq(ghs + x);
  1321. gfs2_holder_uninit(ghs + x);
  1322. }
  1323. out_gunlock_r:
  1324. if (gfs2_holder_initialized(&r_gh))
  1325. gfs2_glock_dq_uninit(&r_gh);
  1326. out:
  1327. return error;
  1328. }
  1329. /**
  1330. * gfs2_exchange - exchange two files
  1331. * @odir: Parent directory of old file name
  1332. * @odentry: The old dentry of the file
  1333. * @ndir: Parent directory of new file name
  1334. * @ndentry: The new dentry of the file
  1335. * @flags: The rename flags
  1336. *
  1337. * Returns: errno
  1338. */
  1339. static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
  1340. struct inode *ndir, struct dentry *ndentry,
  1341. unsigned int flags)
  1342. {
  1343. struct gfs2_inode *odip = GFS2_I(odir);
  1344. struct gfs2_inode *ndip = GFS2_I(ndir);
  1345. struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
  1346. struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
  1347. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1348. struct gfs2_holder ghs[5], r_gh;
  1349. unsigned int num_gh;
  1350. unsigned int x;
  1351. umode_t old_mode = oip->i_inode.i_mode;
  1352. umode_t new_mode = nip->i_inode.i_mode;
  1353. int error;
  1354. gfs2_holder_mark_uninitialized(&r_gh);
  1355. error = gfs2_rindex_update(sdp);
  1356. if (error)
  1357. return error;
  1358. if (odip != ndip) {
  1359. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1360. 0, &r_gh);
  1361. if (error)
  1362. goto out;
  1363. if (S_ISDIR(old_mode)) {
  1364. /* don't move a directory into its subdir */
  1365. error = gfs2_ok_to_move(oip, ndip);
  1366. if (error)
  1367. goto out_gunlock_r;
  1368. }
  1369. if (S_ISDIR(new_mode)) {
  1370. /* don't move a directory into its subdir */
  1371. error = gfs2_ok_to_move(nip, odip);
  1372. if (error)
  1373. goto out_gunlock_r;
  1374. }
  1375. }
  1376. num_gh = 1;
  1377. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1378. if (odip != ndip) {
  1379. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1380. num_gh++;
  1381. }
  1382. gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1383. num_gh++;
  1384. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1385. num_gh++;
  1386. for (x = 0; x < num_gh; x++) {
  1387. error = gfs2_glock_nq(ghs + x);
  1388. if (error)
  1389. goto out_gunlock;
  1390. }
  1391. error = -ENOENT;
  1392. if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
  1393. goto out_gunlock;
  1394. error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
  1395. if (error)
  1396. goto out_gunlock;
  1397. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1398. if (error)
  1399. goto out_gunlock;
  1400. if (S_ISDIR(old_mode)) {
  1401. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1402. if (error)
  1403. goto out_gunlock;
  1404. }
  1405. if (S_ISDIR(new_mode)) {
  1406. error = gfs2_permission(ndentry->d_inode, MAY_WRITE);
  1407. if (error)
  1408. goto out_gunlock;
  1409. }
  1410. error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
  1411. if (error)
  1412. goto out_gunlock;
  1413. error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
  1414. if (error)
  1415. goto out_end_trans;
  1416. error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
  1417. if (error)
  1418. goto out_end_trans;
  1419. error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
  1420. IF2DT(old_mode));
  1421. if (error)
  1422. goto out_end_trans;
  1423. error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
  1424. IF2DT(new_mode));
  1425. if (error)
  1426. goto out_end_trans;
  1427. if (odip != ndip) {
  1428. if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
  1429. inc_nlink(&odip->i_inode);
  1430. drop_nlink(&ndip->i_inode);
  1431. } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
  1432. inc_nlink(&ndip->i_inode);
  1433. drop_nlink(&odip->i_inode);
  1434. }
  1435. }
  1436. mark_inode_dirty(&ndip->i_inode);
  1437. if (odip != ndip)
  1438. mark_inode_dirty(&odip->i_inode);
  1439. out_end_trans:
  1440. gfs2_trans_end(sdp);
  1441. out_gunlock:
  1442. while (x--) {
  1443. gfs2_glock_dq(ghs + x);
  1444. gfs2_holder_uninit(ghs + x);
  1445. }
  1446. out_gunlock_r:
  1447. if (gfs2_holder_initialized(&r_gh))
  1448. gfs2_glock_dq_uninit(&r_gh);
  1449. out:
  1450. return error;
  1451. }
  1452. static int gfs2_rename2(struct inode *odir, struct dentry *odentry,
  1453. struct inode *ndir, struct dentry *ndentry,
  1454. unsigned int flags)
  1455. {
  1456. flags &= ~RENAME_NOREPLACE;
  1457. if (flags & ~RENAME_EXCHANGE)
  1458. return -EINVAL;
  1459. if (flags & RENAME_EXCHANGE)
  1460. return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
  1461. return gfs2_rename(odir, odentry, ndir, ndentry);
  1462. }
  1463. /**
  1464. * gfs2_get_link - Follow a symbolic link
  1465. * @dentry: The dentry of the link
  1466. * @inode: The inode of the link
  1467. * @done: destructor for return value
  1468. *
  1469. * This can handle symlinks of any size.
  1470. *
  1471. * Returns: 0 on success or error code
  1472. */
  1473. static const char *gfs2_get_link(struct dentry *dentry,
  1474. struct inode *inode,
  1475. struct delayed_call *done)
  1476. {
  1477. struct gfs2_inode *ip = GFS2_I(inode);
  1478. struct gfs2_holder i_gh;
  1479. struct buffer_head *dibh;
  1480. unsigned int size;
  1481. char *buf;
  1482. int error;
  1483. if (!dentry)
  1484. return ERR_PTR(-ECHILD);
  1485. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1486. error = gfs2_glock_nq(&i_gh);
  1487. if (error) {
  1488. gfs2_holder_uninit(&i_gh);
  1489. return ERR_PTR(error);
  1490. }
  1491. size = (unsigned int)i_size_read(&ip->i_inode);
  1492. if (size == 0) {
  1493. gfs2_consist_inode(ip);
  1494. buf = ERR_PTR(-EIO);
  1495. goto out;
  1496. }
  1497. error = gfs2_meta_inode_buffer(ip, &dibh);
  1498. if (error) {
  1499. buf = ERR_PTR(error);
  1500. goto out;
  1501. }
  1502. buf = kzalloc(size + 1, GFP_NOFS);
  1503. if (!buf)
  1504. buf = ERR_PTR(-ENOMEM);
  1505. else
  1506. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1507. brelse(dibh);
  1508. out:
  1509. gfs2_glock_dq_uninit(&i_gh);
  1510. if (!IS_ERR(buf))
  1511. set_delayed_call(done, kfree_link, buf);
  1512. return buf;
  1513. }
  1514. /**
  1515. * gfs2_permission -
  1516. * @inode: The inode
  1517. * @mask: The mask to be tested
  1518. * @flags: Indicates whether this is an RCU path walk or not
  1519. *
  1520. * This may be called from the VFS directly, or from within GFS2 with the
  1521. * inode locked, so we look to see if the glock is already locked and only
  1522. * lock the glock if its not already been done.
  1523. *
  1524. * Returns: errno
  1525. */
  1526. int gfs2_permission(struct inode *inode, int mask)
  1527. {
  1528. struct gfs2_inode *ip;
  1529. struct gfs2_holder i_gh;
  1530. int error;
  1531. gfs2_holder_mark_uninitialized(&i_gh);
  1532. ip = GFS2_I(inode);
  1533. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1534. if (mask & MAY_NOT_BLOCK)
  1535. return -ECHILD;
  1536. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1537. if (error)
  1538. return error;
  1539. }
  1540. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1541. error = -EPERM;
  1542. else
  1543. error = generic_permission(inode, mask);
  1544. if (gfs2_holder_initialized(&i_gh))
  1545. gfs2_glock_dq_uninit(&i_gh);
  1546. return error;
  1547. }
  1548. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1549. {
  1550. setattr_copy(inode, attr);
  1551. mark_inode_dirty(inode);
  1552. return 0;
  1553. }
  1554. /**
  1555. * gfs2_setattr_simple -
  1556. * @ip:
  1557. * @attr:
  1558. *
  1559. * Returns: errno
  1560. */
  1561. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1562. {
  1563. int error;
  1564. if (current->journal_info)
  1565. return __gfs2_setattr_simple(inode, attr);
  1566. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1567. if (error)
  1568. return error;
  1569. error = __gfs2_setattr_simple(inode, attr);
  1570. gfs2_trans_end(GFS2_SB(inode));
  1571. return error;
  1572. }
  1573. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1574. {
  1575. struct gfs2_inode *ip = GFS2_I(inode);
  1576. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1577. kuid_t ouid, nuid;
  1578. kgid_t ogid, ngid;
  1579. int error;
  1580. struct gfs2_alloc_parms ap;
  1581. ouid = inode->i_uid;
  1582. ogid = inode->i_gid;
  1583. nuid = attr->ia_uid;
  1584. ngid = attr->ia_gid;
  1585. if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
  1586. ouid = nuid = NO_UID_QUOTA_CHANGE;
  1587. if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
  1588. ogid = ngid = NO_GID_QUOTA_CHANGE;
  1589. error = gfs2_rsqa_alloc(ip);
  1590. if (error)
  1591. goto out;
  1592. error = gfs2_rindex_update(sdp);
  1593. if (error)
  1594. goto out;
  1595. error = gfs2_quota_lock(ip, nuid, ngid);
  1596. if (error)
  1597. goto out;
  1598. ap.target = gfs2_get_inode_blocks(&ip->i_inode);
  1599. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1600. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1601. error = gfs2_quota_check(ip, nuid, ngid, &ap);
  1602. if (error)
  1603. goto out_gunlock_q;
  1604. }
  1605. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1606. if (error)
  1607. goto out_gunlock_q;
  1608. error = gfs2_setattr_simple(inode, attr);
  1609. if (error)
  1610. goto out_end_trans;
  1611. if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
  1612. !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
  1613. gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
  1614. gfs2_quota_change(ip, ap.target, nuid, ngid);
  1615. }
  1616. out_end_trans:
  1617. gfs2_trans_end(sdp);
  1618. out_gunlock_q:
  1619. gfs2_quota_unlock(ip);
  1620. out:
  1621. return error;
  1622. }
  1623. /**
  1624. * gfs2_setattr - Change attributes on an inode
  1625. * @dentry: The dentry which is changing
  1626. * @attr: The structure describing the change
  1627. *
  1628. * The VFS layer wants to change one or more of an inodes attributes. Write
  1629. * that change out to disk.
  1630. *
  1631. * Returns: errno
  1632. */
  1633. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1634. {
  1635. struct inode *inode = d_inode(dentry);
  1636. struct gfs2_inode *ip = GFS2_I(inode);
  1637. struct gfs2_holder i_gh;
  1638. int error;
  1639. error = gfs2_rsqa_alloc(ip);
  1640. if (error)
  1641. return error;
  1642. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1643. if (error)
  1644. return error;
  1645. error = -EPERM;
  1646. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1647. goto out;
  1648. error = setattr_prepare(dentry, attr);
  1649. if (error)
  1650. goto out;
  1651. if (attr->ia_valid & ATTR_SIZE)
  1652. error = gfs2_setattr_size(inode, attr->ia_size);
  1653. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1654. error = setattr_chown(inode, attr);
  1655. else {
  1656. error = gfs2_setattr_simple(inode, attr);
  1657. if (!error && attr->ia_valid & ATTR_MODE)
  1658. error = posix_acl_chmod(inode, inode->i_mode);
  1659. }
  1660. out:
  1661. if (!error)
  1662. mark_inode_dirty(inode);
  1663. gfs2_glock_dq_uninit(&i_gh);
  1664. return error;
  1665. }
  1666. /**
  1667. * gfs2_getattr - Read out an inode's attributes
  1668. * @mnt: The vfsmount the inode is being accessed from
  1669. * @dentry: The dentry to stat
  1670. * @stat: The inode's stats
  1671. *
  1672. * This may be called from the VFS directly, or from within GFS2 with the
  1673. * inode locked, so we look to see if the glock is already locked and only
  1674. * lock the glock if its not already been done. Note that its the NFS
  1675. * readdirplus operation which causes this to be called (from filldir)
  1676. * with the glock already held.
  1677. *
  1678. * Returns: errno
  1679. */
  1680. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1681. struct kstat *stat)
  1682. {
  1683. struct inode *inode = d_inode(dentry);
  1684. struct gfs2_inode *ip = GFS2_I(inode);
  1685. struct gfs2_holder gh;
  1686. int error;
  1687. gfs2_holder_mark_uninitialized(&gh);
  1688. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1689. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1690. if (error)
  1691. return error;
  1692. }
  1693. generic_fillattr(inode, stat);
  1694. if (gfs2_holder_initialized(&gh))
  1695. gfs2_glock_dq_uninit(&gh);
  1696. return 0;
  1697. }
  1698. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1699. u64 start, u64 len)
  1700. {
  1701. struct gfs2_inode *ip = GFS2_I(inode);
  1702. struct gfs2_holder gh;
  1703. int ret;
  1704. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1705. if (ret)
  1706. return ret;
  1707. inode_lock(inode);
  1708. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1709. if (ret)
  1710. goto out;
  1711. if (gfs2_is_stuffed(ip)) {
  1712. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1713. u64 size = i_size_read(inode);
  1714. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1715. FIEMAP_EXTENT_DATA_INLINE;
  1716. phys += sizeof(struct gfs2_dinode);
  1717. phys += start;
  1718. if (start + len > size)
  1719. len = size - start;
  1720. if (start < size)
  1721. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1722. len, flags);
  1723. if (ret == 1)
  1724. ret = 0;
  1725. } else {
  1726. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1727. gfs2_block_map);
  1728. }
  1729. gfs2_glock_dq_uninit(&gh);
  1730. out:
  1731. inode_unlock(inode);
  1732. return ret;
  1733. }
  1734. const struct inode_operations gfs2_file_iops = {
  1735. .permission = gfs2_permission,
  1736. .setattr = gfs2_setattr,
  1737. .getattr = gfs2_getattr,
  1738. .listxattr = gfs2_listxattr,
  1739. .fiemap = gfs2_fiemap,
  1740. .get_acl = gfs2_get_acl,
  1741. .set_acl = gfs2_set_acl,
  1742. };
  1743. const struct inode_operations gfs2_dir_iops = {
  1744. .create = gfs2_create,
  1745. .lookup = gfs2_lookup,
  1746. .link = gfs2_link,
  1747. .unlink = gfs2_unlink,
  1748. .symlink = gfs2_symlink,
  1749. .mkdir = gfs2_mkdir,
  1750. .rmdir = gfs2_unlink,
  1751. .mknod = gfs2_mknod,
  1752. .rename = gfs2_rename2,
  1753. .permission = gfs2_permission,
  1754. .setattr = gfs2_setattr,
  1755. .getattr = gfs2_getattr,
  1756. .listxattr = gfs2_listxattr,
  1757. .fiemap = gfs2_fiemap,
  1758. .get_acl = gfs2_get_acl,
  1759. .set_acl = gfs2_set_acl,
  1760. .atomic_open = gfs2_atomic_open,
  1761. };
  1762. const struct inode_operations gfs2_symlink_iops = {
  1763. .readlink = generic_readlink,
  1764. .get_link = gfs2_get_link,
  1765. .permission = gfs2_permission,
  1766. .setattr = gfs2_setattr,
  1767. .getattr = gfs2_getattr,
  1768. .listxattr = gfs2_listxattr,
  1769. .fiemap = gfs2_fiemap,
  1770. };