inode.c 55 KB

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