I second ulcers, but it could really be anything. The behaviours are a result of him anticipating pain so getting angry or disciplining him won’t help you at all. Finding the source of his pain is key especially if this is relatively new behaviour.
The same thing happened to me with my horse since I got him, I am just very firm with him. He also used to turn around to bite when I was getting on to ride (so I assumed he was feeling pain), so I started giving him some massages through his neck and back and he loves them, I can see where he is hurting by putting pressure on his back, really concentrate on these areas if there are any. Hope this helps!
If the behaviour is new I'd definitely check for ulcers. There's some really good YouTube videos that help identify if a horse has ulcers.
He could be getting territorial, I would work on pairing grooming and petting with treats, just don’t let him make it a habit
The majority of the time grumpyness is related to pain. It could be caused by a number of reasons such as muscle/joint pain or ulcers which many horses have and owners never realise the lists go on! I highly suggest getting an expert in equine Physio and body work or an equine chiropractic to see your horse before turning to discipline
I would also just give him a once over and make sure there's not a underlying reason as to why he is doing this. If he is not tender and all seems okay I would go with Kelbi's advice.
Bring a stick in with you when you groom and give him a tap when he tries to bite. You don’t have to hit him very hard, just a small tap.