inode.c 49 KB

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