inode.c 48 KB

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