inode.c 54 KB

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